[ACCEPTED]-How to know if MySQLnd is the active driver?-mysqlnd

Accepted answer
Score: 61

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

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

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

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

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

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

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

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

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

This code:

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

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

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

A better 4 way to check for mysqli with mysqlnd vs 3 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 2 works because mysqlnd provides three additional functions that work only when mysqlnd is used as the driver.

Finally, the PDO check needs 1 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>';
}
?>
Score: 10

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

Here are the three configure options 7 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 6 "Client API version" is "mysqlnd 5.0.5-dev" for 5 both mysql, mysqli, and pdo_mysql.

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

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

Score: 8

This is what I was looking for

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

0

Score: 3

Maybe check if these settings exist? phpinfo() renders 3 them differently from other ini settings 2 for some reason. Works for 5.4, not sure 1 about 5.3.

ini_get('mysqlnd.debug') !== false
Score: 1

phpinfo() in the beginning lists the "Configure 15 Command" used to compile PHP.

As they 14 state in other answers mysqlnd is 1 (the 13 default) of 2 choices during the php install/compile 12 process.

My phpinfo Configure Command for 11 7.0.33 is:

'./configure' '--prefix=/opt/php70' '--with-libdir=lib64' '--enable-bcmath' '--enable-calendar' '--enable-dbase' '--enable-exif' '--enable-ftp' '--enable-gd-native-ttf' '--enable-intl' '--enable-libxml' '--enable-mbstring' '--enable-pdo' '--enable-soap' '--enable-sockets' '--enable-sqlite-utf8' '--enable-wddx' '--enable-zip' '--with-bz2' '--with-curl' '--with-freetype-dir' '--with-gd' '--with-gettext' '--with-gmp' '--with-imap' '--with-imap-ssl' '--with-jpeg-dir=/usr' '--with-kerberos' '--with-mcrypt' '--with-mhash' '--with-mssql' '--with-mysql=/usr' '--with-mysql-sock=/var/lib/mysql/mysql.sock' '--with-mysqli=/usr/bin/mysql_config' '--with-openssl' '--with-pdo-mysql=/usr' '--with-pdo-pgsql=/usr' '--with-pgsql=/usr' '--with-pdo-sqlite' '--with-png-dir' '--with-pspell' '--with-sqlite' '--with-system-tzdata' '--with-tidy' '--with-unixODBC' '--with-xmlrpc' '--with-xsl' '--with-zlib'

Note 10 --with-mysqli=/usr/bin/mysql_config' '

and 9 --enable-mysqlnd' ' (Not in this one but 8 a readout on a 5.6 php build)

--with-mysqli= is 7 pointing to a directory meaning it is using 6 libmysqlclient If it was mysqlnd instead 5 then it is using the native driver.

For 4 more info on this http://php.net/manual/en/mysqlinfo.library.choosing.php

(I know this is way old 3 however this knowledge would have saved 2 me hours of tech support debate and I seen 1 this first.)

More Related questions