W3docs

ping

In this article, we will focus on the mysqli_ping() function in PHP, which is used to check if a connection to the MySQL server is active.

This article covers the mysqli_ping() function in PHP, which checks whether a connection to the MySQL server is still active and, where supported, transparently reconnects a dropped link. You will see both the object-oriented and procedural syntax, the function signature, when to reach for it, and the important deprecation in PHP 8.4.

What mysqli_ping() does

mysqli_ping() pings a live MySQL connection. If the connection is alive it returns true. If the link is down, the function attempts to reconnect when the mysqli.reconnect configuration directive is enabled, and returns true on a successful reconnect or false if it cannot be re-established.

It is most useful for long-running scripts (workers, daemons, queue consumers) where a connection may sit idle long enough for the server to close it after wait_timeout seconds — the classic "MySQL server has gone away" error. Pinging before a query lets you detect or recover from that situation.

Syntax

// Object-oriented style
$mysqli->ping(): bool

// Procedural style
mysqli_ping(mysqli $mysql): bool

Parameters

  • $mysql (procedural only) — a connection link identifier returned by mysqli_connect() or mysqli_init().

Return valuetrue if the connection is alive (or was successfully reconnected), false otherwise.

Deprecation note: mysqli_ping() and the automatic-reconnect feature are deprecated as of PHP 8.4 and removed in future versions, because silent reconnects can lose session state (temporary tables, prepared statements, transactions, SET variables). The recommended approach is to catch the failed query and open a fresh connection yourself.

Object-oriented example

<?php
$mysqli = new mysqli("localhost", "username", "password", "database");

if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error;
    exit();
}

// ping() automatically attempts to reconnect if the link is dead
if ($mysqli->ping()) {
    echo "Connection is OK!";
} else {
    echo "Error: " . $mysqli->error;
}

$mysqli->close();
?>

The script initializes a MySQLi connection, verifies it succeeded via connect_errno, and then calls ping() to confirm the link is alive. It outputs a success or error message via connect_error accordingly.

Security Note: In production environments, avoid hardcoding database credentials. Use environment variables or secure configuration files to store sensitive data.

Procedural example

For procedural style, you can use mysqli_ping($link) instead:

<?php
$link = mysqli_connect("localhost", "username", "password", "database");

if (!$link) {
    die("Connection failed: " . mysqli_connect_error());
}

if (mysqli_ping($link)) {
    echo "Connection is OK!";
} else {
    echo "Error: " . mysqli_error($link);
}

mysqli_close($link);
?>

Because ping() is deprecated in PHP 8.4, the durable pattern is to run your query, detect the "gone away" failure, and reconnect explicitly:

<?php
function runWithReconnect(callable $makeConnection, string $sql): mysqli_result|bool
{
    $mysqli = $makeConnection();
    try {
        return $mysqli->query($sql);
    } catch (mysqli_sql_exception $e) {
        // MySQL error 2006: server has gone away — reopen and retry once.
        if ($e->getCode() === 2006) {
            $mysqli = $makeConnection();
            return $mysqli->query($sql);
        }
        throw $e;
    }
}
?>

This keeps you in full control of connection state instead of relying on a silent reconnect.

Conclusion

mysqli_ping() is a quick way to check whether a MySQL connection is still alive and, on older PHP versions, to reconnect a dropped link. It is handy in long-running scripts, but since PHP 8.4 it is deprecated — prefer catching the failed query() and reopening the connection yourself. For more on establishing connections, see mysqli_connect() and the MySQLi overview.

Practice

Practice
What is the purpose of a ping in PHP?
What is the purpose of a ping in PHP?
Was this page helpful?