Flames Posted May 31, 2009 Share Posted May 31, 2009 I recently learned OOP and then made a DB class to handle connections etc, and there is a public variable called $con which is true if the database is connected. I have another class which extends my DB class but I seem to be having problems with accessing the $con variable to see if it is connected, how can I do this, I tried using DB::$con but that gave "Fatal error: Access to undeclared static property: Database::$con" Thanks Quote Link to comment Share on other sites More sharing options...
anupamsaha Posted May 31, 2009 Share Posted May 31, 2009 I recently learned OOP and then made a DB class to handle connections etc, and there is a public variable called $con which is true if the database is connected. I have another class which extends my DB class but I seem to be having problems with accessing the $con variable to see if it is connected, how can I do this, I tried using DB::$con but that gave "Fatal error: Access to undeclared static property: Database::$con" Thanks Do this: public static $conn; Does this help? Quote Link to comment Share on other sites More sharing options...
Flames Posted May 31, 2009 Author Share Posted May 31, 2009 The error has gone but it still isnt getting the right value, it should have the value true but it is saying it is false Quote Link to comment Share on other sites More sharing options...
anupamsaha Posted May 31, 2009 Share Posted May 31, 2009 The error has gone but it still isnt getting the right value, it should have the value true but it is saying it is false Please post the DB class here. Quote Link to comment Share on other sites More sharing options...
Flames Posted May 31, 2009 Author Share Posted May 31, 2009 class Database { private $db_host = ''; private $db_user = ''; private $db_pass = ''; private $db_name = ''; public static $con; public function connect() { if(!$this->con) { $myconn = @mysql_connect($this->db_host,$this->db_user,$this->db_pass); if($myconn) { $seldb = @mysql_select_db($this->db_name,$myconn); if($seldb) { $this->con = true; return true; } else { return false; } } else { return false; } } else { return true; } } public function disconnect() { if($this->con) { if(@mysql_close()) { $this->con = false; return true; } else { return false; } } } } class Login extends Database { public function checkDatabase($field, $check) { if(Database::$con) { $query = "SELECT * FROM Users where $field = '$check'"; $result = mysql_query($query) or die("error");; if(mysql_num_rows($result)>0) { return "no"; } else { return "yes"; } } else { return "not connected"; } } } Quote Link to comment Share on other sites More sharing options...
anupamsaha Posted May 31, 2009 Share Posted May 31, 2009 I see the db informations (the private variables) are blank in the class. How are setting those? Directly in the class? If not, create a constructor in the class to set those and then use those in the connect() method. As they are blank in the class, you get "false" from the connect() method. <?php /** * Database.php */ class Database { private $db_host = ''; private $db_user = ''; private $db_pass = ''; private $db_name = ''; public static $con; /** * Magic construct method */ public function __construct($host, $user, $password, $dbname) { $this->$db_host = $host; $this->db_user = $user; $this->db_pass = $password; $this->db_name = $dbname; } public function connect() { if(!$this->con) { $myconn = @mysql_connect($this->db_host,$this->db_user,$this->db_pass); if($myconn) { $seldb = @mysql_select_db($this->db_name,$myconn); if($seldb) { $this->con = true; return true; } else { return false; } } else { return false; } } else { return true; } } public function disconnect() { if($this->con) { if(@mysql_close()) { $this->con = false; return true; } else { return false; } } } } ?> To call this class: <?php /** * dbtest.php */ include('Database.php'); $db = new Database('localhost', 'username', 'password', 'database_name'); var_dump($db->connect()); ?> Quote Link to comment Share on other sites More sharing options...
jxrd Posted May 31, 2009 Share Posted May 31, 2009 parent::$con; Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 31, 2009 Share Posted May 31, 2009 /* Add to database class... */ protected function isConnected() { return $this->con; } /* Login Class */ class Login extends Database { public function checkDatabase($field, $check) { if(parent::isConnected()) Quote Link to comment Share on other sites More sharing options...
Flames Posted May 31, 2009 Author Share Posted May 31, 2009 It still returns not connected but there are no errors now Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 31, 2009 Share Posted May 31, 2009 $login = new Login('localhost', 'user', 'pass', 'dbName'); print_r($login); What does that return? Quote Link to comment Share on other sites More sharing options...
Flames Posted May 31, 2009 Author Share Posted May 31, 2009 Login Object ( [db_host:private] => XXXXXXX [db_user:private] => XXXXXXX [db_pass:private] => XXXXXXX[db_name:private] => XXXXXXX[con] => ) Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 31, 2009 Share Posted May 31, 2009 Have you tried calling the checkDatabase method yet? Also I dont see any use in creating a static variable without assigning a value. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 31, 2009 Share Posted May 31, 2009 Well, for one, your DB connect method is not written correctly. By the way, you can connect without selecting a DB. Not sure of your logic there. public function connect() { if(!self::$con) { $myconn = @mysql_connect($this->db_host,$this->db_user,$this->db_pass); if($myconn) { $seldb = @mysql_select_db($this->db_name,$myconn); if($seldb) { self::$con = true; return true; } } self::$con = false; return false; } else { return true; } } After you did that, replace if(Database::$con) With if(parent::connect()) Since your DB connect method returns a boolean, you can just do that instead of accessing member data publicly. This way, you can set the $con var in DB to private. Quote Link to comment Share on other sites More sharing options...
Flames Posted May 31, 2009 Author Share Posted May 31, 2009 thanks for the help guys Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.