mysql_ping() function in PHP

What is mysql_ping() function in PHP?
How does mysql_ping() works?


The mysql_ping() function pings a server connection or reconnects to it if there is no connection.
bool mysql_ping ( [resource link_identifier])

Returns TRUE if there is a connection, or FALSE on failure.
mysql_ping() function is used to check whether the connection to the server is working or not. If the connection to the server gone down, an automatic reconnection to the server is attempted by this function. This function can be used by scripts that remain idle for a long while, to check whether the server has closed the connection and reconnect to server if necessary.

//Attempt to connect to the default database server
$conn = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
//select database
$db = mysql_select_db('my_database');
// Assuming this query will take a long time
$result = mysql_query($sql);
if (!$result) {
echo 'Query #1 failed, exiting.';
// Make sure the connection is still alive, if not, try to reconnect
if (!mysql_ping($conn)) {
echo 'Lost connection';
// So the connection is still alive, let's run another query
$result2 = mysql_query($sql2);

See also: mysql_thread_id() and mysql_list_processes().

PHP Topics

Ask Questions

Ask Question