PHP mysqli_get_links_stats() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_get_links_stats() returns an associative array with counts of mysqlnd-tracked mysqli links, such as total, active and inactive connections.

It requires mysqlnd and takes no connection argument. Typical keys include total, active_connections and active_persistent. Use it when debugging connection churn in long-running PHP processes.

Syntax

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

PHP
$links = mysqli_get_links_stats();
CallWhat it does
mysqli_get_links_stats()Returns mysqlnd link-count statistics.
$links["total"]Reads how many links mysqlnd is tracking in the sample.
$links["active_connections"]Reads the active connection count from the array.

Needs mysqlnd. Values are about PHP's mysqli links in this process, not about every connection on the MySQL server. Pair with get_client_stats() when you need broader mysqlnd counters.

Library declarations
PHP
mysqli_get_links_stats(): array

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
arrayAssociative array of link statistics from mysqlnd.

PHP Examples

Example 1 — Read Link Counts

Call mysqli_get_links_stats() and print common keys from a fixed sample.

PHP
<?php
$links = mysqli_get_links_stats();

echo "total=", $links["total"], PHP_EOL;
echo "active_connections=", $links["active_connections"], PHP_EOL;

How It Works

  1. No connections have been opened yet in the sample.
  2. get_links_stats() returns the mysqlnd link array.
  3. total and active_connections are both 0 here.
  4. Opening a mysqli link raises these counts.

Example 2 — Counts After Opening a Connection

Connect once, then show active_connections as 1 in the demo output.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$links = mysqli_get_links_stats();

echo "active_connections=", $links["active_connections"], PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. mysqli_connect() creates one active link.
  2. get_links_stats() reflects that link.
  3. The sample active count is 1.
  4. Closing the link afterward drops the active count.

Example 3 — Include Persistent Key

Print the active_persistent key as well for a fuller snapshot.

PHP
<?php
$links = mysqli_get_links_stats();
echo "total=", $links["total"],
     " active=", $links["active_connections"],
     " persistent=", $links["active_persistent"], PHP_EOL;

How It Works

  1. Three related counters are read from the array.
  2. A quiet process shows zeros in the sample.
  3. Persistent links would raise active_persistent.
  4. Useful when teaching p: host prefixes and persistence.

Frequently Asked Questions

It returns mysqlnd statistics about how many mysqli links exist in the current PHP process.
Yes. Link tracking stats come from the MySQL Native Driver.
No. The function takes no parameters and reports process-wide link counts.
get_links_stats() counts links. get_connection_stats() returns detailed counters for one specific connection.
No. It only knows about mysqli links created inside this PHP process through mysqlnd.

Next: mysqli_get_proto_info()

mysqli_get_proto_info() returns the MySQL protocol version number used by an open connecti.

mysqli_get_proto_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