TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Installing.. (http://www.talkphp.com/absolute-beginners/1210-installing.html)

Tanax 09-23-2007 04:11 PM

Installing..
 
Hey, I got a problem again >.<

PHP Code:

include('core/database.php');
    $step = $_GET['step'];
    
    ?>
    <div class="page">
        <div id="pagetitle">
            <h1>Installing...</h1>
            <hr>
        </div>
        
        <div id="pagestep">
        
    <?php
    
    
switch($step) {
        
        case 
1:
        
        if(!isset(
$_POST['connect'])) {
            
        
?>
        
        <p>Step 1: Database connection</p>
        </div>
        <div id="pageform">
        <form action="<?php $_SERVER['PHPSELF']; ?>" method="POST">
        <div id="pagecat2">Database host:</div>
        <input type="text" name="host" value="localhost">
        <div id="pagecat2">Database user:</div>
        <input type="text" name="user" value="root">
        <div id="pagecat2">Database password:</div>
        <input type="text" name="pass">
        <div id="pagecat2">Database name:</div>
        <input type="text" name="data">
        <input type="submit" value="Connect" name="connect">
        </form></div>
        <?php
        
        
} elseif(isset($_POST['connect'])) {
            
            
$user $_POST['user'];
            
$host $_POST['host'];
            
$pass $_POST['pass'];
            
$data $_POST['data'];
            
            
$test = new    database($host$user$pass$data);
            if(!
$test) {
                
                
?>
                <p>Step 1: Database connection</p>
                <div id="pagecat2">Failed! Check your database again for the correct data.</div>
                <a href="install.php?step=1">Go back</a>
                
                <?php
                
            
}
            
            else {
                
                
?>
                <p>Step 1: Database connection</p>
                <div id="pagecat2">Success! Please click the link in order to proceed.</div>
                <a href="install.php?step=2">Step 2</a>
                
                <?php
                
                $file 
'config.php';
                
$open fopen($file'a');
                
$content 
'<?php 
                
function __autoload($class_name) { 
                    
    require_once \'core/\' . $class_name . \'.php\'; 
                    

                
$system->db = new database(\''
.$host.'\', \''.$user.'\', \''.$pass.'\', \''.$data.'\');';
                            
                
$write fwrite($open$content);
                
fclose($open);
                
            }
        }
        
    }

Basicly, it's this line:
PHP Code:

$test = new    database($host$user$pass$data);
            if(!
$test) { 

What I'm trying to do here is that if the database connection attempt is not successful, then it should do the things inside the brackets of the if(!$test) { }

Database class is like this:
PHP Code:

public function __construct($host$user$pass$db) {
                
            
$this->db['host'] = $host;
            
$this->db['user'] = $user;
            
$this->db['pass'] = $pass;
            
$this->db['db'] = $db;
                
            
$this->connect();
                        
        }

private function 
connect() {
            
            
$connect = @mysql_connect($this->db['host'], $this->db['user'], $this->db['pass']);
            
            if(!
$connect) {
                
                return 
false;
                
            }
            
            else {
                
                
$this->select();
                
            }
              
        }

private function 
select() {
            
            
$select = @mysql_select_db($this->db['db']);
            
            if(!
$select) {
                
                return 
false;
                
            }
            
            else {
                
                return 
true;
                
            }
            
        } 


Wildhoney 09-23-2007 05:24 PM

Looks right to me. What's it not doing?

Tanax 09-23-2007 05:38 PM

The problem is that even if I write a database that doesn't exists, it gives me successful, and writes the config.php file. Which, it shouldn't.

It should only write it if the connecting with the database is successful.

Karl 09-23-2007 06:19 PM

The problem, as you correctly stated, is this line:

PHP Code:

$test = new    database($host$user$pass$data);
            if(!
$test) { 

I can see that you're testing to see if the database connection was successfull, but what you're actually testing is if the database object exists. The easiest solution (without changing alot of code) would be to simply remove the $this->connect() call from your class' __construct() method and call it after instantiating your class object, changing the current code to:

PHP Code:

$test = new    database($host$user$pass$data);
            if(!
$test->connect()) { 

You will also need to change the scope of the connect() function from private to public.

Tanax 09-23-2007 06:22 PM

Ah! Thanks! I'll try it asap :)

Tanax 09-23-2007 06:30 PM

I got this:
Quote:

Catchable fatal error: Object of class database could not be converted to string in C:\wamp\www\DB Class\install.php on line 56
PHP Code:

if(!$$test->connect()) { 

(line 56)

Edit, ooops!

Saw the error.

Now:
Quote:

Failed! Check your database again for the correct data.
:D:D Thanks!


All times are GMT. The time now is 03:43 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0