1

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

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Marc B. Thanks, I've tried what you suggested but if the connection is closed i get: Warning: mysql_ping(): 13 is not a valid MySQL-Link resource in ... I've used this [link]stackoverflow.com/questions/5148033/… to check. How I release resources, close connection?
if mysql_close() returns true, then the connection's closed. and obvious, since you've closed the connectin, $res isn't a valid handle anymore. it's still a handle, it's just not usable anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.