PHP mysqli_ping() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_ping() checks whether the MySQL connection is still alive and returns true if the server responds.

Older PHP builds could auto-reconnect on ping; that reconnect behavior is gone in modern PHP, and mysqli_ping() itself is deprecated as of PHP 8.4. For long-lived scripts, handle disconnects by reconnecting explicitly.

Syntax

Call mysqli_ping() with the values you need. Start with this line.

PHP
$alive = mysqli_ping($mysqli);
CallWhat it does
mysqli_ping($mysqli)Returns true if the connection still answers.
if (!mysqli_ping($mysqli)) { /* reconnect */ }Branches when the server connection is gone.
var_dump(mysqli_ping($mysqli))Prints bool(true) or bool(false).

Beginners writing short request/response scripts rarely need ping. Open a connection, run queries, close. Ping matters more in workers or CLI daemons that keep a handle open for a long time.

Library declarations
PHP
mysqli_ping(mysqli $mysql): bool

Parameters

ParameterDescription
mysqlRequired. The mysqli connection to check.

Return Value

Return ValueDescription
booltrue if the connection is alive; false if the ping failed.

PHP Examples

Example 1 — Ping a Fresh Connection

A newly opened connection normally answers the ping. Sample output is fixed.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");

var_dump(mysqli_ping($mysqli));

mysqli_close($mysqli);

How It Works

  1. Connect with placeholder credentials.
  2. mysqli_ping() asks the server if the link still works.
  3. true means the connection is alive in this sample.
  4. Close afterward.

Example 2 — Ping After Close

Once closed, the handle is no longer a live connection.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
mysqli_close($mysqli);

// After close, ping is not meaningful for new work — reconnect instead.
echo "Connection closed; open a new one if needed.", PHP_EOL;

How It Works

  1. mysqli_close() ends the session.
  2. Do not keep using the old handle.
  3. Create a new mysqli_connect() when you need the database again.
  4. This is clearer than relying on deprecated ping/reconnect behavior.

Example 3 — Long Script Pattern Without Ping

Show an explicit reconnect approach for modern PHP.

PHP
<?php
function db_connect()
{
    return mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
}

$mysqli = db_connect();
// ... long work ...
if (!$mysqli) {
    $mysqli = db_connect();
}

echo $mysqli ? "Ready." : "Unavailable.", PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. Wrap connect in a small helper.
  2. If the handle is missing or unusable, call the helper again.
  3. This replaces old ping-and-auto-reconnect habits.
  4. Short web requests usually connect once per request.

Frequently Asked Questions

It checks whether the MySQL connection is still alive and returns true or false.
Yes, as of PHP 8.4. Prefer explicit reconnect logic for long-running scripts.
Auto-reconnect via ping is not available on modern PHP. Treat ping as a liveness check only where it still exists.
Usually no. Connect, query, close is enough for a single request.
Open a new connection and retry the operation carefully, rather than depending on mysqli_ping().

Next: mysqli_poll()

mysqli_poll() waits until one or more asynchronous MySQLi connections have results ready, .

mysqli_poll() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful