Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
Thread: can't close mySQL connection
08-05-2008, 06:42 AM #1
- Join Date
- Jul 2007
- Thanked 0 Times in 0 Posts
can't close mySQL connection
I'm trying to close my database connection at the end of each file, but I get an error when I do so:
Warning: mysql_close(): 5 is not a valid MySQL-Link resource in C:\xampp\htdocs\website\DbConnect.class.php on line 61PHP Code:
$db = new DbConnector;
// Website content goes here
// System variables
$settings['siteDir'] = '/public_html/website/';
// Database variables
$settings['dbhost'] = 'localhost';
$settings['dbusername'] = 'user';
$settings['dbpassword'] = 'password';
$settings['dbname'] = 'database';
class DbConnector extends SystemComponent
//*** Function: DbConnector, Purpose: Connect to the database ***
// Load settings from parent class
$settings = SystemComponent::getSettings();
// Get the main settings from the array we just loaded
$host = $settings['dbhost'];
$db = $settings['dbname'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];
// Connect to the database
$this->link = mysql_connect($host, $user, $pass);
//*** Function: close, Purpose: Close the connection ***
Last edited by Bob42; 08-05-2008 at 07:37 AM.
08-05-2008, 07:36 AM #2
- Join Date
- Jul 2002
- Vancouver, BC Canada
- Thanked 100 Times in 100 Posts
I think by default that the connection closes after the query, so try leaving out $db->close();
08-05-2008, 08:33 AM #3
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,659 Times in 2,628 Posts
Its because you're resource is not open.
I believe len is mostly right (at least this is the case in .net), if you query a recordset and the connection is closed, it will open the connection, query for data, and then close it. If the connection is previously opened, it will query for data without closing it. In PHP mysql_close is almost unnecessary since the close is called on script termination anyway.
Consider upgrading to PHP5 object model though, it has better control of you're scopes. register shutdown functions are unnecessary since php5 has a destruct method. You can shut you're warning up by surpressing it with an @ sign in front of the mysql_close call.
header('HTTP/1.1 420 Enhance Your Calm');
08-05-2008, 12:24 PM #4
- Join Date
- Oct 2006
- Denver, Colorado USA
- Thanked 304 Times in 296 Posts
There are two possibilities. Either your connection failed (you have no error checking logic in your code so you will never know if the connection worked or not) or this is being caused when php is cleaning up after script execution
For the first possibility, you should be checking if a connection was created and don't attempt to close it if there is not a connection.
For the second possibility, when php is cleaning up at the end of the script execution, it destroys all resources used by the script, including database connection links. It then calls any functions registered using register_shutdown_function(). So, at the time your close function is called due to the shutdown function, a link would not exist anyway.
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.