How to know if MySQLnd is the active driver?

The driver (libmysql or mysqlnd) is choosen at compile-time, and each one of those two can be specified independatly for mysql, mysqli, and pdo_mysql.

Here are the three configure options that correspond to mysqlnd :

  --with-mysql[=DIR]      Include MySQL support.  DIR is the MySQL base
                          directory.  If mysqlnd is passed as DIR,
                          the MySQL native driver will be used [/usr/local]
  --with-mysqli[=FILE]    Include MySQLi support.  FILE is the path
                          to mysql_config.  If mysqlnd is passed as FILE,
                          the MySQL native driver will be used [mysql_config]
  --with-pdo-mysql[=DIR]    PDO: MySQL support. DIR is the MySQL base directoy
                                 If mysqlnd is passed as DIR, the MySQL native
                                 native driver will be used [/usr/local]


In your case, the "Client API version" is "mysqlnd 5.0.5-dev" for both mysql, mysqli, and pdo_mysql.

So it seems you ahre using mysqlnd in either three cases.

In the case of PDO, you have the MySQL driver installed -- and that one is compiled based on mysqlnd.


Warning! This method is unreliable and does not work since PHP 8.1

If you are accessing via mysqli, this should do the trick:

<?php
$mysqlnd = function_exists('mysqli_fetch_all');

if ($mysqlnd) {
    echo 'mysqlnd enabled!';
}

To detect if its the active PDO driver, create your MySQL PDO object then:

if (strpos($pdo->getAttribute(PDO::ATTR_CLIENT_VERSION), 'mysqlnd') !== false) {
    echo 'PDO MySQLnd enabled!';
}

This is what I was looking for

<?php
if (extension_loaded('mysqlnd')) {
}
?>

Checking for mysqli_fetch_all does not really describe wether you are using mysqlnd. Rather, it says that you have the mysqli extension enabled.

MySQLi is simply an updated version of the mysql extension that was provided in earlier versions of PHP.

The mysql extension, the mysqli extension and the PDO MySQL driver can each be individually configured to use either libmysqlclient or mysqlnd

This code:

<?php
$mysqlnd = function_exists('mysqli_fetch_all');

if ($mysqlnd) {
    echo 'mysqlnd enabled!';
}

not echoing nothing suggests that you don't have mysqli compiled/enabled/installed, and might be using the older mysql extension.

A better way to check for mysqli with mysqlnd vs mysql with libmysqlclient is to do this:

<?php
if (function_exists('mysql_connect')) {
    echo "- MySQL <b>is installed</b>.<br>";
} else  {
    echo "- MySQL <b>is not</b> installed.<br>";
}

if (function_exists('mysqli_connect')) {
    echo "- MySQLi <b>is installed</b>.<br>";
} else {
    echo "- MySQLi <b>is not installed</b>.<br>";
}

if (function_exists('mysqli_get_client_stats')) {
    echo "- MySQLnd driver is being used.<br>";
} else {
    echo "- libmysqlclient driver is being used.<br>";
}

This works because mysqlnd provides three additional functions that work only when mysqlnd is used as the driver.

Finally, the PDO check needs to have the $pdo variable defined first.

$dbHost = "localhost";
$dbUser = "root";
$dbPass = "password";
$dbName = "database";

$pdo = new PDO('mysql:host='.$dbHost.';dbname='.$dbName, $dbUser, $dbPass);
if (strpos($pdo->getAttribute(PDO::ATTR_CLIENT_VERSION), 'mysqlnd') !== false) {
    echo '- PDO MySQLnd <b>is enabled</b>.<br>';
} else {
    echo '- PDO MySQLnd <b>is not enabled</b>.<br>';
}
?>

Tags:

Mysql

Php

Mysqlnd