60

Maybe it's an obvious question, but I want to be sure.

How can i know if it MySQLnd is the active driver?

I'm runing PHP 5.3 and MySQL 5.1.37. In phpinfo() mysqlnd is listed but only with this I can't be sure if I'm using MySQLnd or the old driver...

Extract of phpinfo() output

mysql
MySQL Support   enabled
Active Persistent Links     0
Active Links    0
Client API version  mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.27 $ 

mysqli
MysqlI Support  enabled
Client API library version  mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.27 $
Active Persistent Links     0
Inactive Persistent Links   0
Active Links    26 

mysqlnd
mysqlnd enabled
Version     mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.27 $ 

PDO
PDO support enabled
PDO drivers     mysql

pdo_mysql
PDO Driver for MySQL    enabled
Client API version  mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.27 $ 

I'm using PDO, and PDO driver says mysql...

Charles
  • 50,943
  • 13
  • 104
  • 142
The Disintegrator
  • 4,147
  • 9
  • 35
  • 43
  • 3
    Yes the "PDO drivers" row says "mysql", but below that it says "Client API version **mysqlnd** 5.0.5-dev - 081106 - $Revision: 1.3.2.27 $"........ – Pacerier Oct 18 '14 at 08:17

6 Answers6

60

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!';
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Inspire
  • 2,052
  • 15
  • 14
  • 2
    It looks like I'm having a similar problem and when i try to use the getAttribute function i get this error: Call to a member function `getAttribute()` on a non-object. It appears that mysqlnd is enabled but i can't use `get_result()` either, any idea? – cooking good Feb 19 '14 at 18:45
  • This function isn't robust enough since it will give a false negative if `mysqli` itself isn't even available. See http://stackoverflow.com/a/22499259/632951 for a better version. – Pacerier Oct 18 '14 at 08:44
  • @Inspire, And another possible issue is of course if someone else defines that function even while you don't have mysqlnd. Then you would get a false positive. – Pacerier Jun 29 '15 at 08:40
  • @cookinggood The variable `$pdo` must be an instance of `PDO`. That is, you must assign `new PDO(...)` to it, first. – caw Mar 18 '17 at 16:55
  • 1
    This is wrong. As @Tim-Groeneveld suggest check for a `mysqlnd` specific function. – Antonio Bardazzi Oct 16 '19 at 12:58
  • @AntonioBardazzi - Tim appears to be (for now) wrong: [mysqli-fetch-all doc](https://www.php.net/manual/en/mysqli-result.fetch-all.php) explicitly says **"MySQL Native Driver Only .. Available only with mysqlnd"**. I read that as saying that checking for `mysqli_fetch_all` **does** only succeed when you are using mysqlnd. It is a valid test - at least for now; in theory that could change in future. – ToolmakerSteve Aug 24 '20 at 23:41
  • .. though to clarify, any check of a *mysqli* function only tells us whether *mysqli* is using `mysqlnd`; to know whether `PDO` is using `mysqlnd`, use the second code snippet in this answer. – ToolmakerSteve Aug 24 '20 at 23:52
  • @ToolmakerSteve PHP 8.1 supports `mysqli_fetch_all` when linked against `libmysqlclient`. The correct way to test for mysqlnd is with my method. – Tim Groeneveld Jul 08 '22 at 06:17
  • As mentioned by others, this answer is no longer valid. Tim Groeneveld's https://stackoverflow.com/a/22499259/1045774 should be marked as the accepted answer – dregad May 10 '23 at 11:09
44

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>';
}
?>
emotality
  • 12,795
  • 4
  • 39
  • 60
Tim Groeneveld
  • 8,739
  • 3
  • 44
  • 60
  • Undefined variable: sentence in C:\Dev\Billing\checkmysqlnd.php on line 8 .. either need to put `$sentence = ""` at top or have the first `if / else` changed from `.=` to `=` .. still works just throws that error – J-Dizzle Sep 17 '14 at 17:11
  • Re *"Checking for mysqli_fetch_all does not really describe wether you are using mysqlnd .."*. Actually [mysqli-fetch-all doc](https://www.php.net/manual/en/mysqli-result.fetch-all.php) explicitly says **"MySQL Native Driver Only .. Available only with mysqlnd"**. I read that as saying that checking for `mysqli_fetch_all` **does** describe whether you are using mysqlnd. – ToolmakerSteve Aug 24 '20 at 23:38
  • ... though reading your answer further, I realize (any of the *mysqli* checks) only tells us whether **mysqli** is using `mysqlnd`; for `PDO` it is the last part of your answer. Thanks. – ToolmakerSteve Aug 24 '20 at 23:51
10

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.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 1
    Sorry, I didn't understood your last sentence. "MySQL driver installed -- and that one is compiled based on mysqlnd." It means it's using mysqlnd or not? BTW I'm ussing debian, I don't want to compile and lose the possibility to use apt-get – The Disintegrator Sep 25 '09 at 07:07
  • PDO is using "drivers" : one for SQLite, one for Postgre, one for mssql, one for MySQL, ... And, for MySQL, that driver can be compiled to use either libmysql or mysqlnd ;; in your case, it says "mysqlnd" – Pascal MARTIN Sep 25 '09 at 10:11
  • @PascalMARTIN, Are you saying that the value to the right of the key `PDO drivers` (in his phpinfo output) can never be **mysqli** and can only be **mysql** (, postgre, sqllite, etc)? – Pacerier Jun 29 '15 at 08:50
  • @Pacerier - given that `PDO` is an *alternative to* `mysqli`, that surely must be the case; it would not be meaningful to have "PDO built on mysqli", right? – ToolmakerSteve Aug 24 '20 at 23:23
8

This is what I was looking for

<?php
if (extension_loaded('mysqlnd')) {
}
?>
mikeytown2
  • 1,744
  • 24
  • 37
3

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

ini_get('mysqlnd.debug') !== false
Bad Habit
  • 31
  • 3
  • It will return empty `string(0) ""` if mysqlnd installed, but may not active (PHP8.0) – a55 Mar 25 '21 at 03:20
1

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

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

My phpinfo Configure Command for 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 --with-mysqli=/usr/bin/mysql_config' '

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

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

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

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

JSG
  • 390
  • 1
  • 4
  • 13