Current Location: Home> Function Categories> mysql_ping

mysql_ping

Ping A server connection, reconnect if there is no connection.
Name:mysql_ping
Category:Uncategorized
Programming Language:php
One-line Description:Check if the connection to the MySQL server still exists, if the connection is disconnected, try to reconnect

Function name: mysql_ping()

Applicable version: PHP 4, PHP 5, PHP 7

Function description: the mysql_ping() function is used to check whether the connection to the MySQL server still exists. If the connection is disconnected, try to reconnect.

Syntax: bool mysql_ping ( resource $link_identifier = NULL )

parameter:

  • $link_identifier (optional): MySQL connection identifier. If this parameter is not provided, the most recently opened connection will be used.

Return value: Return true if the connection still exists or the reconnection is successful; otherwise return false.

Example:

 // 创建MySQL连接$link = mysql_connect("localhost", "username", "password"); // 检查连接是否仍然存在if (mysql_ping($link)) { echo "连接仍然存在"; } else { echo "连接断开,尝试重新连接"; mysql_close($link); // 关闭断开的连接// 重新连接$link = mysql_connect("localhost", "username", "password"); if (mysql_ping($link)) { echo "重新连接成功"; } else { echo "重新连接失败"; } } // 关闭连接mysql_close($link);

In the above example, first, the connection to the MySQL server is created through the mysql_connect() function. Then use the mysql_ping() function to check whether the connection still exists. If the connection is disconnected, the disconnected connection is closed through the mysql_close() function, and try to reconnect by re-calling the mysql_connect() function. Finally, close the connection using the mysql_close() function.

Note that since PHP 5.5.0, the mysql extension has been deprecated and has been removed in PHP 7.0.0. It is recommended to use mysqli or PDO extensions instead of mysql extensions.

Similar Functions
Popular Articles