Descrierea

boolmysql_ping
([ resource$link_identifier = NULL
] )

Checks whether or not the connection to
the server is working. If it has gone down, an automatic reconnection is
attempted. This function can be used by scripts that remain idle for a
long while, to check whether or not the server has closed the connection
and reconnect if necessary.

Notă:

Automatic reconnection is disabled by default in versions of MySQL >= 5.0.3.

When checking if a $resource works...be prepared that mysql_ping returns NULL as long as $resource is no correct mysql resource.<?php$resource =NULL;var_dump = @mysql_ping($resource);# showing NULL?>This could be used to decide of a current $resource is a mysql or a mysqli connection when nothing else is available to do that...

When using the mysql_ping command under php 5.1.2 and mysql 5.0, I was having problems with the auto-reconnect "feature", mainly that when the connection was severed, a mysql_ping would not automatically re-establish the connection to the database.

The connection to the DB is dropped when the time without a query excedes the wait_timeout value in my.cnf. You can check your wait_timeout by running the query "SHOW VARIABLES;"

If you're having problems auto-reconnecting when the connection is dropped, use this code:

if (!mysql_ping ($conn)) {//here is the major trick, you have to close the connection (even though its not currently working) for it to recreate properly.mysql_close($conn);$conn = mysql_connect('localhost','user','pass');mysql_select_db('db',$conn);}

It should be noted that mysql_ping() seems to reset the error message on the server.I used it to check whether the connection was still alive before reading the error message via mysql_error() and it always returned an empty string. Upon removing the connection check everything worked.

Is important to remember that if your first connection to mysql don't works, mysql_ping will always return true! So, if you want to check if mysql is connected, first of all you must check if mysql_connect do not returns false and then you can begin to check mysql_ping.

If you get 'error 2006: MySQL server has gone away' messages when running (really) long scripts, mysql_ping will help detecting the loss of the db-connection. This can happen, when 'wait timeout' is reached (MySQL default is 8 hours).