...

View Full Version : can't close mySQL connection



Bob42
08-05-2008, 07:42 AM
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 61



require('DbConnect.class.php');
$db = new DbConnector;

// Website content goes here

$db->close();


And now for the class...



class SystemComponent
{
var $settings;

function getSettings()
{
// System variables
$settings['siteDir'] = '/public_html/website/';

// Database variables
$settings['dbhost'] = 'localhost';
$settings['dbusername'] = 'user';
$settings['dbpassword'] = 'password';
$settings['dbname'] = 'database';

return $settings;
}
}

class DbConnector extends SystemComponent
{
var $theQuery;
var $link;

//*** Function: DbConnector, Purpose: Connect to the database ***
function DbConnector()
{
// 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);
mysql_select_db($db);
register_shutdown_function(array(&$this, 'close'));
}

//*** Function: close, Purpose: Close the connection ***
function close()
{
mysql_close($this->link);
}
}


So how do I fix this?

Len Whistler
08-05-2008, 08:36 AM
I think by default that the connection closes after the query, so try leaving out $db->close();



---------

Fou-Lu
08-05-2008, 09:33 AM
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.

CFMaBiSmAd
08-05-2008, 01:24 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum