View Single Post
Old 07-02-2008, 08:44 PM   #5 (permalink)
drewbee
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

to expand even further on this, your class variables are not being utilized.
These guys:
PHP Code:
private $dbn;
private 
$user;
private 
$pass
The variables inside of the function ($dbn, $user, and $pass) are all local to the function, and will no longer be usable outside of the function. You need to assign them to use them outside of it.

Also, constructers are very useful as it will execute as soon as the object is initialized. I ahve the constructor called below to call the two functions. The constructor can be either called __constructor or the exact name of the class in this case: db_connect

PHP Code:
<?php
 
 
class db_connect 
{
 
    private 
$dbn;
    private 
$user;
    private 
$pass;
    
    public function 
db_connect()
    {
        
$this->specs('localhost''username''password');
        
$this->showConnectionDetails();
    }
 
    public function 
specs($dbn$user$pass
    {
        
$this->dbn $dbn;
        
$this->user $user;
        
$this->pass $pass;
 
        
$dbtestcon mysql_connect($dbn$user$pass);
        if (!
$dbtestcon
        {
            die(
'Could not connect: ' mysql_error());
        }
        echo 
'Connected successfully';
    }
 
    
// Now this function will work
    
function showConnectionDetails()
    {
        echo 
'We connected on ' $this->dbn ' with username: ' $this->user ' and password ' $this->password;
    }
 
}
 
?>
PHP Code:
$database_connection = new db_connect();
// Will output:
// 'connected succesfully'
// We connected on 'localhost' with username: 'username' and password 'password' 
__________________
There are No Stupid Questions. But there a LOT of Inquisitive Idiots.
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
The Following User Says Thank You to drewbee For This Useful Post:
codefreek (07-02-2008)