get_connection_stats
In this article, we will focus on the mysqli_get_connection_stats() function in PHP, which is used to return the statistics for a MySQL connection. We will
The mysqli_get_connection_stats() function returns a detailed snapshot of low-level statistics about a MySQL connection — bytes transferred, query counts, buffer sizes, and dozens of other counters collected by the underlying mysqlnd driver. This page explains what the function returns, when it is genuinely useful, and how to read the most important values.
What mysqli_get_connection_stats() does
mysqli_get_connection_stats() returns an associative array of runtime statistics for a single, already-open MySQLi connection. The counters come from mysqlnd (the MySQL Native Driver), which is the default driver bundled with PHP since PHP 5.4, so the function is available on every modern PHP installation as long as mysqlnd is in use.
Its signature is:
mysqli_get_connection_stats(mysqli $mysql): array- Parameter —
$mysqlis a validmysqliobject returned bymysqli_connect()(procedural) ornew mysqli(...)(object-oriented). - Return value — an array of
string => int|stringpairs. It returnsfalseonly if the driver cannot supply statistics (effectively never on a normal mysqlnd build).
A key distinction: these are per-connection statistics. If you want process-wide totals across every connection in the current PHP request, use mysqli_get_client_stats() instead.
How to use it
Open a connection, then pass it to the function. Both the procedural and object-oriented styles work:
<?php
// Procedural style
$mysqli = mysqli_connect("localhost", "username", "password", "database");
if (!$mysqli) {
die("Connection failed: " . mysqli_connect_error());
}
$stats = mysqli_get_connection_stats($mysqli);
print_r($stats);
mysqli_close($mysqli);<?php
// Object-oriented style — identical result
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_errno) {
die("Connection failed: " . $mysqli->connect_error);
}
$stats = $mysqli->get_connection_stats();
print_r($stats);
$mysqli->close();print_r() dumps the full array, which has 160+ entries. A trimmed sample looks like this:
Array
(
[bytes_sent] => 43
[bytes_received] => 80
[packets_sent] => 1
[packets_received] => 2
[connect_success] => 1
[com_query] => 0
[rows_fetched_from_server_normal] => 0
[result_set_queries] => 0
...
)Reading the most useful counters
You rarely need all 160 values. These are the ones worth watching:
| Counter | Meaning |
|---|---|
bytes_sent / bytes_received | Total payload moved over the wire, in bytes. Useful for spotting oversized result sets. |
packets_sent / packets_received | Protocol packet counts — a high ratio to bytes can hint at chatty round-trips. |
connect_success / connect_failure | How many connection attempts succeeded or failed on this handle. |
com_query | Number of statements sent with COM_QUERY (i.e. non-prepared queries). |
rows_fetched_from_server_normal | Rows the server sent for buffered results — a quick way to catch accidental full-table reads. |
result_set_queries | Queries that produced a result set. |
Because most of these values are integers, you can pull a single one out of the array directly:
<?php
$stats = mysqli_get_connection_stats($mysqli);
echo "Bytes received so far: " . $stats['bytes_received'] . PHP_EOL;When to use it
Reach for mysqli_get_connection_stats() when you are:
- Profiling data transfer — confirming a query isn't pulling far more rows or bytes than expected.
- Debugging connection behavior — checking
connect_failureor reconnect-related counters when a connection seems unstable. - Building lightweight diagnostics — logging a few counters after a request to track database load over time.
It is a read-only inspection tool: calling it never changes the connection or the data. For general connection setup and teardown, see mysqli_connect() and mysqli_close(); for error details, see mysqli_connect_error().
Conclusion
mysqli_get_connection_stats() exposes mysqlnd's per-connection counters as a plain array, giving you an easy window into how much data a connection has moved and how many queries it has run. Combined with mysqli_get_client_stats() for request-wide totals, it's a handy, zero-cost way to profile and debug your MySQL interactions.