PHP 7.2.0 Release Candidate 4 Released


(PHP 4 >= 4.3.0, PHP 5)

mysql_pingPing 一个服务器连接,如果没有连接则重新连接


bool mysql_ping ([ resource $ link_identifier ] )

mysql_ping() 检查到服务器的连接是否正常。如果断开,则自动尝试连接。本函数可用于空闲很久的脚本来检查服务器是否关闭了连接,如果有必要则重新连接上。如果到服务器的连接可用则 mysql_ping() 返回 TRUE,否则返回 FALSE

参见 mysql_thread_id()mysql_list_processes()

add a note add a note

User Contributed Notes 7 notes

dustin hawkins
11 years ago
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:


= mysql_connect('localhost','user','pass');

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.
$conn = mysql_connect('localhost','user','pass');

//run queries knowing that your connection is alive....

alext at marketdream dot com dot mx
8 years ago
mysql_ping() is really helpful when you have this annoying error:

MYSQL Error 2006 Server has gone away

For CI users:
In 1.7.2 version of codeigniter, there is a function


that uses mysql_ping() to reestablish the timed out connection.

This function is specially useful when developing social media sites that uses hundreds of connections to the db such asinserting or selecting.
miro dot dietiker at md-systems dot ch
10 years ago
When checking if a $resource works...
be prepared that mysql_ping returns NULL as long as $resource is no correct mysql resource.
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...
cybot2000 at yahoo dot de
12 years ago
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.
vinicius at teracom dot com dot br
13 years ago
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.
7 years ago
This function *does not* attempt to reconnect at this time.  It only tells you whether or not you currently *are* connected.

To actually reconnect, you will have to implement this yourself in a wrapper class.
8 years ago
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).
To Top