PHP mysqli_get_connection_stats() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_get_connection_stats() returns an associative array of mysqlnd statistics for one open mysqli connection.

It requires mysqlnd. Pass the connection you want to inspect. Keys overlap with client stats but apply to that link only. The object style is $mysqli->get_connection_stats().

Syntax

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

PHP
$stats = mysqli_get_connection_stats($mysqli);
CallWhat it does
mysqli_get_connection_stats($mysqli)Returns mysqlnd stats for that connection.
$mysqli->get_connection_stats()OO form of the same call.
$stats["bytes_received"]Reads one per-connection counter from the array.

Needs mysqlnd. Without mysqlnd this API is not available in the usual form. Use it to compare traffic or result activity on a specific connection during debugging.

Library declarations
PHP
mysqli_get_connection_stats(mysqli $mysql): array

Parameters

ParameterDescription
mysqlRequired. An open mysqli connection created with mysqlnd.

Return Value

Return ValueDescription
arrayAssociative array of per-connection mysqlnd statistics.

PHP Examples

Example 1 — Read Stats for One Connection

Connect, run a tiny query, then print sample connection counters.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
mysqli_query($mysqli, "SELECT 1");
$stats = mysqli_get_connection_stats($mysqli);

echo "bytes_sent=", $stats["bytes_sent"], PHP_EOL;
echo "bytes_received=", $stats["bytes_received"], PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. A demo connection is opened.
  2. SELECT 1 produces a little client/server traffic.
  3. get_connection_stats() returns that link's counters.
  4. Sample byte counts are printed.

Example 2 — Object Style Call

Use $mysqli->get_connection_stats() after connecting.

PHP
<?php
$mysqli = new mysqli("localhost", "demo", "secret", "shop");
$stats = $mysqli->get_connection_stats();

echo array_key_exists("bytes_sent", $stats) ? "ok" : "missing", PHP_EOL;
$mysqli->close();

How It Works

  1. new mysqli() opens the connection with mysqlnd.
  2. get_connection_stats() returns the array.
  3. We check that a known key exists.
  4. The sample prints ok.

Example 3 — Compare Two Queries on One Link

Capture bytes_received before and after a second query in a fixed demo.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
mysqli_query($mysqli, "SELECT 1");
$before = mysqli_get_connection_stats($mysqli)["bytes_received"];

mysqli_query($mysqli, "SELECT id, name FROM products LIMIT 3");
$after = mysqli_get_connection_stats($mysqli)["bytes_received"];

echo "before=", $before, " after=", $after, PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. A first query establishes a baseline counter.
  2. A wider SELECT increases bytes_received.
  3. Both sample values are printed.
  4. Useful when teaching that stats grow with traffic.

Frequently Asked Questions

It returns mysqlnd statistics for a single mysqli connection as an associative array.
Yes. Per-connection stats come from the MySQL Native Driver.
Client stats are global to the mysqlnd client. Connection stats belong to one mysqli link.
Call $mysqli->get_connection_stats().
No. You must pass an open mysqli connection.

Next: mysqli_get_host_info()

mysqli_get_host_info() returns a string describing the host and connection type used by an.

mysqli_get_host_info() 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