Klaujesi
Klaujesi

Reputation: 1836

PHP do not close MySQL connection

I'm using WAMP Server and I've a simple PHP script:

include_once ('../lib/conecData.php');

$res=mysql_connect(DB_HOST,DB_USER,DB_PASS);
mysql_select_db(DB_NAME);

$sql = "SELECT * FROM `table` ORDER BY `day` DESC";
$result = mysql_query ($sql, $res);
mysql_close($res);

if ($res) echo "Still Alive"; else echo "Closed";

result:

Still Alive

I've tried: 1./----

if($res == false){
    echo "Closed";
} else {
     echo "Still Alive";
}

2./----

$closed = mysql_close($res);
if ( !$closed ) echo "Still Alive"; else echo "Closed";

3./----

if ( mysql_close($res)) echo "Still Alive"; else echo "Closed";

and

mysql_close();

no luck, same result.- Why is database connected?. Do I need to change something on my server's configuration?. What's wrong with this?.

Upvotes: 1

Views: 75

Answers (1)

Marc B
Marc B

Reputation: 360572

$res is a mysql handle. The value in that handle is NOT something you can use to test if the connection is open or closed. Closing the connection does NOT delete the handle, and whatever is in $res will remain - and be a "non-false" value.

In other words, you're testing a completely useless value for something that it can't be tested for.

if you want to confirm that the connection was closed, then

$status = mysql_ping($res);

$status will be TRUE if a connection is open, false otherwise.

Upvotes: 3

Related Questions