Wuhtzu Posted January 31, 2007 Share Posted January 31, 2007 Hey guysI have this very simple mysql class one of my friends wrote, but I am having problems with the close() function and I believe he has too if he tries to use it :)[code]class mysql { public $connection; function connect(){ $host = "lolol"; $username = "lolol"; $password = "lolol"; $database = "lolol"; $connection = mysql_connect($host, $username, $password) or die(mysql_error()); mysql_select_db("$database", $connection) or die(mysql_error()); } function close(){ mysql_close($this->connection) or die(mysql_error()); } more functions that are irrelavant... }[/code]If I then try something like this:[code]$mysql = new mysql;$mysql->connect();$mysql->close();[/code]I get the following error:Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in ...\classes\mysql.php on line 20Line 20 is "mysql_close($this->connection) or die(mysql_error());" and don't worry, the connect() works fine - I can connect to my db without problem and insert data...So what have I/he done wrong and how to fix it?Best regardsWuhtzu Link to comment https://forums.phpfreaks.com/topic/36437-mysql-class-problems-with-close/ Share on other sites More sharing options...
Jessica Posted January 31, 2007 Share Posted January 31, 2007 "Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in ...\classes\mysql.php on line 20"So $this->connection must not be a valid resource. Wheres the code you use to define it? Link to comment https://forums.phpfreaks.com/topic/36437-mysql-class-problems-with-close/#findComment-173387 Share on other sites More sharing options...
Wuhtzu Posted January 31, 2007 Author Share Posted January 31, 2007 Here is the code I was testing, it's very simple and the solo purpose is to open a connection and close it again:[code] $mysql = new mysql;$mysql->connect();$mysql->close();[/code]And if I just use the mysql related code the mysql-class is made of it works fine:[code] $host = "localhost"; $username = "dan16068_engrosb"; $password = "2D4unGas"; $database = "dan16068_engrosbilar"; $connection = mysql_connect($host, $username, $password) or die(mysql_error()); if($connection){ echo "Succesfull connection"; } $close = mysql_close($connection); if($close){ echo "Connection closed"; }[/code]So the problem is how to get a valid MySQL-link resource into my mysql_close() .... Link to comment https://forums.phpfreaks.com/topic/36437-mysql-class-problems-with-close/#findComment-173581 Share on other sites More sharing options...
Wuhtzu Posted January 31, 2007 Author Share Posted January 31, 2007 Just to mention it, I have tried with both:[code]class mysql { public $connection;[/code]and[code]class mysql { var $connection;[/code] Link to comment https://forums.phpfreaks.com/topic/36437-mysql-class-problems-with-close/#findComment-173587 Share on other sites More sharing options...
paul2463 Posted January 31, 2007 Share Posted January 31, 2007 not much used to classes and writing them but it does not make sense to me to make a call to $this->connection when the connections is $connection so should it not be[code]function close(){ mysql_close($this->$connection) or die(mysql_error()); }[/code] Link to comment https://forums.phpfreaks.com/topic/36437-mysql-class-problems-with-close/#findComment-173590 Share on other sites More sharing options...
Wuhtzu Posted January 31, 2007 Author Share Posted January 31, 2007 Thanks for you suggestion paul2463, but $this->variable refers to a variable defined inside the class using "var $variable" and $this->$variable is incorrect :) Link to comment https://forums.phpfreaks.com/topic/36437-mysql-class-problems-with-close/#findComment-173598 Share on other sites More sharing options...
Wuhtzu Posted January 31, 2007 Author Share Posted January 31, 2007 no one? Link to comment https://forums.phpfreaks.com/topic/36437-mysql-class-problems-with-close/#findComment-173723 Share on other sites More sharing options...
Jessica Posted January 31, 2007 Share Posted January 31, 2007 So when you do Can you try this?function close(){ print $this->connection; mysql_close() or die(mysql_error());}It should say something like Resource ID #, right?You also don't need to supply the connection, so try it without. Link to comment https://forums.phpfreaks.com/topic/36437-mysql-class-problems-with-close/#findComment-173740 Share on other sites More sharing options...
Wuhtzu Posted February 1, 2007 Author Share Posted February 1, 2007 Well it works if I do not supply the connection to close()... but it would be nice to be more specific by supplying the connecting :) Link to comment https://forums.phpfreaks.com/topic/36437-mysql-class-problems-with-close/#findComment-174989 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.