PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




mysql_ping() function in PHP


Tutorials »Php »

Topic

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




Explanation

The mysql_ping() function pings a server connection or reconnects to it if there is no connection.

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

Example


<?php
set_time_limit(0);

//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.';
exit;
}

// Make sure the connection is still alive, if not, try to reconnect
if (!mysql_ping($conn)) {
echo 'Lost connection';
exit;
}

mysql_free_result($result);

// So the connection is still alive, let's run another query
$result2 = mysql_query($sql2);
?>



See also: mysql_thread_id() and mysql_list_processes().





Other Links

web hosting