Current Location: Home> Latest Articles> Check $errno after mysqli_ping to confirm whether the connection is restored

Check $errno after mysqli_ping to confirm whether the connection is restored

M66 2025-05-28

When using MySQL databases, we often need to deal with the situation where the database connection is disconnected. Usually, we do connection detection in the code, and when the database connection is disconnected, we can try to reconnect. A common way is to use the mysqli_ping() function to detect whether the connection is valid and use mysqli::$errno to check the status of the connection recovery.

1. What is mysqli_ping() ?

mysqli_ping() is a function provided by PHP to detect whether a MySQL database connection is still valid. If the database connection is valid, the function returns true , otherwise it returns false . If the connection is not available, you can use this function to try to re-establish the connection.

Example of usage of mysqli_ping()

 <?php
$mysqli = new mysqli("m66.net", "username", "password", "dbname");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// use mysqli_ping() Check if the connection is still valid
if (!$mysqli->ping()) {
    echo "Disconnection,Trying to reconnect...<br>";
    // Try to reconnect
    $mysqli = new mysqli("m66.net", "username", "password", "dbname");

    // Check if the reconnection is successful
    if ($mysqli->connect_error) {
        die("重新Connection failed: " . $mysqli->connect_error);
    } else {
        echo "Reconnect successfully!<br>";
    }
} else {
    echo "The connection remains valid!<br>";
}
?>

In the above code, we use mysqli_ping() to detect the status of the database connection. If the connection is disconnected, we try to reconnect to the database and check if the connection is successful.

2. How to use mysqli::$errno to confirm whether the database connection is restored to normal?

mysqli::$errno is a commonly used property in PHP that contains the error code for the latest database operation. If no error occurred in the recent operation, the value of mysqli::$errno is 0 . If the database connection is restored successfully, mysqli::$errno will also be 0. If a connection failure or other database error occurs, mysqli::$errno will return a non-zero value, indicating the specific cause of the error.

Confirm the connection recovery status using mysqli::$errno

 <?php
$mysqli = new mysqli("m66.net", "username", "password", "dbname");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// use mysqli_ping() Check if the connection is valid
if (!$mysqli->ping()) {
    echo "Disconnection,Trying to reconnect...<br>";
    
    // Try to reconnect
    $mysqli = new mysqli("m66.net", "username", "password", "dbname");

    // use mysqli::$errno Confirm whether the connection is restored
    if ($mysqli->connect_error) {
        echo "重新Connection failed,Error code: " . $mysqli->connect_errno . "<br>";
    } else {
        echo "Reconnect successfully!<br>";
    }
} else {
    echo "The connection remains valid!<br>";
}
?>

In the above code, we confirm whether the connection is restored by checking mysqli::$errno . When trying to reconnect, if there is a connection error, you can get the specific error code through $mysqli->connect_errno to help developers understand the problem.

3. Why use it in combination with mysqli_ping() and mysqli::$errno ?

Although mysqli_ping() can help us check whether the database connection is valid, when the connection is disconnected, there may be some network errors or problems on the MySQL server side. At this point, mysqli::$errno can provide additional error information to help us better diagnose the problem.

For example, mysqli::$errno may return the following error code:

  • 2002 - Connection timeout

  • 1045 - Permissions Issues

  • 2003 - Unable to access the database server

By combining mysqli_ping() and mysqli::$errno , we can have a clearer understanding of the status of database connections and take corresponding measures.

4. Summary

Using mysqli_ping() to check the database connection status and confirm whether it is restored to normal through mysqli::$errno is a very effective way to deal with database connection problems. Combining these two can help us quickly detect and deal with problems with database connections, ensuring the stability and reliability of the program.

Through the above method, we can easily ensure that the connection to the MySQL database is in a healthy state and reduce errors caused by connection loss. Remember, timely detection and handling of database connection issues is the key to ensuring the stable operation of the system.