kartul Posted January 22, 2010 Share Posted January 22, 2010 I'm new to PHP OOP. Currently I'm trying to make some simple classes to understand how they work in real life applications and maybe create something with these testing classes I'm trying to make. Right now I'm stuck with first class and have no idea how do solve the problems. Class I created is simple and should make connection to database and disconnect. It works - half of it works. It makes connection to database (I can retrieve records) but I think disconnecting part doesn't work as it should. Code I have so far: <?php class db_connect { // declare hostname, username, password and db name private $host, $user, $pass, $db_name; // set variables function __construct($host, $user, $pass, $db_name) { $this->host = $host; $this->user = $user; $this->pass = $pass; $this->db_name = $db_name; } // makes db connection. returns true if there is already connection made else tries to make it function connect() { if(!$this->con) { // if no connection ($con is false) make one $c = mysql_connect($this->host, $this->user, $this->pass); $s = mysql_select_db($this->db_name, $c); // check if connection and db is ok if($c && $s) { // they are. $con true $this->con = true; return true; } else { // they are not fine. returns false return false; } } else { return false; } } function disconnect() { // if there is connection then disconnect if($this->con) { // close connection $dis = mysql_close(); // make $con false $this->con = false; //return if clause true return true; } } } ?> <?php include("class.db_connect.php"); $db = new db_connect("localhost", "root", "", "funhouse"); $db->connect(); $sql = mysql_query("SELECT * FROM db LIMIT 1, 30") or die(mysql_error()); while($row = mysql_fetch_assoc($sql)) { echo $row['name'] . "<br>"; } #$db->disconnect(); ?> As you can see, I've commented out disconnecting part. This is because when this line is uncommented, I get This webpage is not available. The webpage at http://www.localhost/dev/test.php/ might be temporarily down or it may have moved permanently to a new web address. I also get notice: Notice: Undefined property: db_connect::$con in C:\wamp\www\dev\class.db_connect.php on line 25 It's this line: if(!$this->con). So my question is - What am I doing wrong? Why do I get 'The webpage is not available' and how do fix this notice? Quote Link to comment https://forums.phpfreaks.com/topic/189451-php-oop-connecting-and-disconnecting-to-mysql/ Share on other sites More sharing options...
KevinM1 Posted January 22, 2010 Share Posted January 22, 2010 Like the error states, your object doesn't have a property named $con. You need to declare it with the rest of your database properties (like you did with $host, $pass, etc.) before you can use it. Quote Link to comment https://forums.phpfreaks.com/topic/189451-php-oop-connecting-and-disconnecting-to-mysql/#findComment-1000016 Share on other sites More sharing options...
kartul Posted January 22, 2010 Author Share Posted January 22, 2010 Like the error states, your object doesn't have a property named $con. You need to declare it with the rest of your database properties (like you did with $host, $pass, etc.) before you can use it. Ok. I declared this like all others. Now the notice is gone. But still, disconnecting part doesn't work. It loads quite long and then displays same stuff - 'This webpage is not available.'. Any idea how do fix this..? Oh, and also, I should close db connection at bottom of every page, right? Quote Link to comment https://forums.phpfreaks.com/topic/189451-php-oop-connecting-and-disconnecting-to-mysql/#findComment-1000017 Share on other sites More sharing options...
KevinM1 Posted January 22, 2010 Share Posted January 22, 2010 Like the error states, your object doesn't have a property named $con. You need to declare it with the rest of your database properties (like you did with $host, $pass, etc.) before you can use it. Ok. I declared this like all others. Now the notice is gone. But still, disconnecting part doesn't work. It loads quite long and then displays same stuff - 'This webpage is not available.'. Any idea how do fix this..? Oh, and also, I should close db connection at bottom of every page, right? Do a test - do you get the same error when not using your object to handle the db connection? Quote Link to comment https://forums.phpfreaks.com/topic/189451-php-oop-connecting-and-disconnecting-to-mysql/#findComment-1000020 Share on other sites More sharing options...
kartul Posted January 22, 2010 Author Share Posted January 22, 2010 Like the error states, your object doesn't have a property named $con. You need to declare it with the rest of your database properties (like you did with $host, $pass, etc.) before you can use it. Ok. I declared this like all others. Now the notice is gone. But still, disconnecting part doesn't work. It loads quite long and then displays same stuff - 'This webpage is not available.'. Any idea how do fix this..? Oh, and also, I should close db connection at bottom of every page, right? Do a test - do you get the same error when not using your object to handle the db connection? Wow.. mysql_close() wanted the variable I assigned to mysql_connect().. Now it works. Thank you!!! Quote Link to comment https://forums.phpfreaks.com/topic/189451-php-oop-connecting-and-disconnecting-to-mysql/#findComment-1000041 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.