7

Db connectivity is handled by an include.

I want to run this command: mysql_close($con);

Only if we currently have a connection. Something like isset()?

Thanks Hamad

4 Answers 4

8
is_resource($con)

gives false if the connection is closed.

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

1 Comment

You can add && get_resource_type($con) === 'mysql link' to make sure is a mysql link resource.
2

You can use mysql_ping($con);

Depending on your PHP version, the older ones would reopen the connection if it was closed automatically. It shouldn't do that in PHP5

1 Comment

From the Manual: Warning This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_ping()
0

You can test the connection by doing a simple mysql_ping, which will re-open the connection if it's closed.

Comments

0

I give the credit to @Artefacto, but I ran into a problem where my $con wasn't set and I received a message such as:

Notice: Undefined variable: con in...

so I changed to:

if ( is_resource($con)) {
     mysqli_close($con);
}

Please note that the mysql_ping($con) is now Deprecated.

Comments

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.