开发者

Check if db connection is closed - php

Db connectivity is handl开发者_开发技巧ed by an include.

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

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

Thanks Hamad


is_resource($con)

gives false if the connection is closed.


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜