Click to See Complete Forum and Search --> : closing database connection


jrthor2
02-04-2008, 06:56 AM
I have an include on my pages that connects to my database, and an include at the bottom of my pages that closes this connection. I am getting an error on the server that says:

[04-Feb-2008 07:51:28] PHP Warning: mysql_close(): 5 is not a valid MySQL-Link resource in /home/goldengl/public_html/inc/closedb.php on line 2

I'm not sure why I'm getting this. Here is my open connection and closing connection code:

Open connection include

$dbhost = "localhost";
$db = "goldengl_inet";
$username = "goldengl_username";
$password = "password";
$connect = mysql_connect($dbhost, $username, $password) or die('Error connecting to mysql');
mysql_select_db($db);

Closing connection include

mysql_close($connect);

Thanks

NogDog
02-04-2008, 03:39 PM
Do you reuse the $connect variable somewhere in the script? For debugging, you might want to put in the following just before the mysql_close():

echo "<pre>";
var_dump($connect);
echo "</pre>";

In any case, my first suggestion would be to just remove the mysql_close() entirely. If you are waiting until the end of the script to close it, then you really aren't gaining anything since the connection will automatically be closed upon script termination. If you really feel a need to do it anyway, you can just leave out the $connect parameter, and it will automatically try to close the last connection opened by the script.