简体   繁体   中英

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?.

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM