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_statis not the filesystemstat()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.000The fields mean:
| Field | Meaning |
|---|---|
Uptime | Seconds the server has been running |
Threads | Currently open client connections |
Questions | Statements executed since startup |
Slow queries | Queries that exceeded long_query_time |
Opens | Tables the server has opened |
Open tables | Tables currently open |
Queries per second avg | Average query throughput |
Syntax
// Procedural style
mysqli_stat(mysqli $connection): string|false
// Object-oriented style
$connection->stat(): string|falseIt 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: 0Now $stats['Uptime'] and $stats['Slow queries'] are easy to threshold, alert on, or graph.
Common use cases
- Liveness check. Call
mysqli_statin a health-check endpoint; if it returnsfalse, the database is unreachable. - Lightweight monitoring. Sample
ThreadsandQueries per second avgon a schedule to spot load spikes. - Catching slow queries. A rising
Slow queriescount is an early signal that an index is missing.
Gotchas
- It returns a string, not an array.
var_dumpon 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 booleanfalse. Use a strict comparison (=== false) so you do not confuse it with an empty string. - It is a snapshot, not history. Values like
Questionsare cumulative since server start; to measure rate you must sample twice and subtract. - Needs a valid connection. If the connection failed, calling
mysqli_staton it will warn or error. Always verify the connection first — see mysqli_connect_error.
Related functions
- mysqli_connect — open the connection
mysqli_statneeds. - mysqli_get_server_info — the server's version string.
- mysqli_ping — check (and optionally reconnect) a connection.
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.