07-02-2008, 10:04 PM
|
#9 (permalink)
|
|
The Acquainted
Join Date: May 2008
Posts: 175
Thanks: 9
|
PHP Code:
// A few things... see comments in code <?php error_reporting(E_ALL & ~E_NOTICE); class db_connect { private $dbn; private $user; private $pass; private $db_selected; public function db_connect() { // you added a 4th parameter to specs. you would need to pass it here // public function specs($dbn, $user, $pass, $db_selected) $this->specs('localhost', 'orb', '123123'); $this->showConnectionDetails(); } public function specs($dbn, $user, $pass, $db_selected) { $this->db_selected = $db_selected; $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() { // We have no idea what the variable 'dbtestcon' is. Remember scope. // $dbtestcon scope is limited to the function spec(); // You can handle this two ways: 1) ommit the identifier (php is smarty enough to know which // connection you are referring to so you could use: // $db_selected = mysql_select_db('zone'); // Option two is to create a class variable for $dbtestcon // 1) create variable: private $dbtestcon; // 2) in function specs() when doing the initial mysql_connect(), assign it to the new var: // $this->dbtestcon = mysql_connect($dbn, $user, $pass); // 3) Reference it below // $db_selected = mysql_select_db('zone', $this->dbtestcon); $db_selected = mysql_select_db('zone', $dbtestcon); // Also, one other note. Since you added the variable 'db_selected', you should use that as well IE // $db_selected = mysql_select_db($this->db_selected, $this->dbtestcon); if (!$db_selected) { die ('Can\'t use workspace : ' . mysql_error()); }
} }
?>
__________________
There are No Stupid Questions. But there a LOT of Inquisitive Idiots.
|
|
|