Bob42
08-05-2008, 06: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?
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?