W3docs

A Comprehensive Guide on mysqli_stat Function in PHP

When it comes to working with MySQL databases in PHP, the mysqli extension provides a variety of functions to perform various operations. One such function is

When you work with MySQL databases in PHP, the mysqli extension exposes many functions for talking to the server. mysqli_stat is the simple one for health checks: it asks the MySQL server for a single line of live status — uptime, how many queries it has run, how many threads are open, and so on.

This guide explains what mysqli_stat returns, how to call it in both the procedural and object-oriented styles, how to turn its raw output into usable numbers, and the common gotchas to watch for.

mysqli_stat is not the filesystem stat() function. If you are looking for file metadata (size, permissions, inode), see stat in PHP and fstat instead.

What mysqli_stat does

mysqli_stat is a built-in function that returns the current status of the MySQL server as one space-separated string. It is the PHP equivalent of running mysqladmin status on the command line — a quick snapshot, not a full performance dump.

A typical return value looks like this:

Uptime: 272701  Threads: 1  Questions: 18  Slow queries: 0  Opens: 17  Flush tables: 1  Open tables: 5  Queries per second avg: 0.000

The fields mean:

FieldMeaning
UptimeSeconds the server has been running
ThreadsCurrently open client connections
QuestionsStatements executed since startup
Slow queriesQueries that exceeded long_query_time
OpensTables the server has opened
Open tablesTables currently open
Queries per second avgAverage query throughput

Syntax

// Procedural style
mysqli_stat(mysqli $connection): string|false

// Object-oriented style
$connection->stat(): string|false

It takes the connection link returned by mysqli_connect (procedural) or a mysqli object (OOP), and returns the status string on success or false on failure (for example, when the connection has been lost).

Basic usage

Procedural style

First connect, then read the status. Always treat a false return as an error.

<?php

$connection = mysqli_connect('localhost', 'username', 'password', 'mydatabase');

if (!$connection) {
    die('Connection failed: ' . mysqli_connect_error());
}

$status = mysqli_stat($connection);

if ($status === false) {
    echo 'Error: ' . mysqli_error($connection);
} else {
    echo $status;
}

Object-oriented style

The same call as a method on the mysqli object:

<?php

$mysqli = new mysqli('localhost', 'username', 'password', 'mydatabase');

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

echo $mysqli->stat();

Parsing the status into numbers

The raw string is fine for logging, but for monitoring you usually want individual values. Each field is Name: value, separated by two spaces, so you can split it into a key/value map:

<?php

function parseMysqlStat(string $status): array
{
    $result = [];

    foreach (explode('  ', $status) as $pair) {
        // Each pair looks like "Uptime: 272701"
        [$key, $value] = array_map('trim', explode(':', $pair, 2));
        $result[$key] = $value;
    }

    return $result;
}

$status = 'Uptime: 272701  Threads: 1  Questions: 18  Slow queries: 0  Opens: 17  Flush tables: 1  Open tables: 5  Queries per second avg: 0.000';

$stats = parseMysqlStat($status);

echo 'Uptime in seconds: ' . $stats['Uptime'] . PHP_EOL;
echo 'Active threads: ' . $stats['Threads'] . PHP_EOL;
echo 'Slow queries: ' . $stats['Slow queries'] . PHP_EOL;

Output:

Uptime in seconds: 272701
Active threads: 1
Slow queries: 0

Now $stats['Uptime'] and $stats['Slow queries'] are easy to threshold, alert on, or graph.

Common use cases

  • Liveness check. Call mysqli_stat in a health-check endpoint; if it returns false, the database is unreachable.
  • Lightweight monitoring. Sample Threads and Queries per second avg on a schedule to spot load spikes.
  • Catching slow queries. A rising Slow queries count is an early signal that an index is missing.

Gotchas

  • It returns a string, not an array. var_dump on the result shows a single string. Parse it yourself (see above) before doing math on the values.
  • Check for false, not empty. On a dropped connection the function returns the boolean false. Use a strict comparison (=== false) so you do not confuse it with an empty string.
  • It is a snapshot, not history. Values like Questions are cumulative since server start; to measure rate you must sample twice and subtract.
  • Needs a valid connection. If the connection failed, calling mysqli_stat on it will warn or error. Always verify the connection first — see mysqli_connect_error.

Conclusion

mysqli_stat is a small but practical tool for monitoring a MySQL server from PHP. It returns a single status string with uptime, thread, and query metrics. Check the result against false to detect a dead connection, and parse the string into a key/value map when you need the individual numbers for health checks or dashboards.

Practice

Practice
What does mysqli_stat() return in PHP?
What does mysqli_stat() return in PHP?
Was this page helpful?