php 如何知道 MySQLnd 是否是活动驱动程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1475701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:45:06  来源:igfitidea点击:

How to know if MySQLnd is the active driver?

phpmysqlmysqlnd

提问by The Disintegrator

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

也许这是一个显而易见的问题,但我想确定一下。

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

我怎么知道它是否 MySQLnd 是活动的驱动程序?

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...

我正在运行 PHP 5.3 和 MySQL 5.1.37。在 phpinfo() 中列出了 mysqlnd 但仅此而已我无法确定我使用的是 MySQLnd 还是旧驱动程序...

Extract of phpinfo() output

phpinfo() 输出的提取

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...

我正在使用 PDO,而 PDO 驱动程序说 mysql...

回答by Inspire

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:

要检测它是否是活动的 PDO 驱动程序,请创建您的 MySQL PDO 对象,然后:

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

回答by Tim Groeneveld

Checking for mysqli_fetch_alldoes not really describe wether you are using mysqlnd. Rather, it says that you have the mysqli extensionenabled.

检查mysqli_fetch_all并不能真正描述您是否正在使用mysqlnd. 相反,它表示您启用了mysqli 扩展

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

MySQLi 只是mysqlPHP 早期版本中提供的扩展的更新版本。

The mysqlextension, the mysqliextension and the PDO MySQL drivercan each be individually configured to use either libmysqlclient or mysqlnd

mysql延长,mysqli延伸和PDO MySQL driver可各自被单独地配置为使用的libmysqlclient或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 mysqlextension.

没有回显任何内容表明您没有编译/启用/安装 mysqli,并且可能正在使用较旧的mysql扩展。

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

使用 mysqlnd 与 mysql 使用 libmysqlclient 检查 mysqli 的更好方法是执行以下操作:

<?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.

这是有效的,因为mysqlnd 提供了三个附加功能,这些功能仅在将 mysqlnd 用作驱动程序时才起作用

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

最后,PDO 检查需要$pdo首先定义变量。

$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>';
}
?>

回答by mikeytown2

This is what I was looking for

这就是我要找的

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

回答by Pascal MARTIN

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.

驱动程序(libmysql 或 mysqlnd)是在编译时选择的,这两者中的每一个都可以为 mysql、mysqli 和 pdo_mysql 单独指定。

Here are the three configure options that correspond to mysqlnd :

以下是与 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.


在您的情况下,mysql、mysqli 和 pdo_mysql 的“客户端 API 版本”都是“mysqlnd 5.0.5-dev”。

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

因此,在这三种情况下,您似乎都在使用 mysqlnd。

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

在 PDO 的情况下,您已经安装了 MySQL 驱动程序——并且该驱动程序是基于 mysqlnd 编译的。

回答by Bad Habit

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

也许检查这些设置是否存在?由于某种原因,phpinfo() 以不同于其他 ini 设置的方式呈现它们。适用于 5.4,不确定是否适用于 5.3。

ini_get('mysqlnd.debug') !== false

回答by JSG

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

开头的 phpinfo() 列出了用于编译 PHP 的“配置命令”。

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

正如他们在其他答案中所述,在 php 安装/编译过程中,mysqlnd 是 2 个选项中的 1 个(默认值)。

My phpinfo Configure Command for 7.0.33 is:

我的 phpinfo 7.0.33 配置命令是:

'./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'

'./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.袜子''--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' '

注意 --with-mysqli=/usr/bin/mysql_config' '

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

和 --enable-mysqlnd' ' (不在这个中,而是在 5.6 php 版本中的读数)

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

--with-mysqli= 指向一个目录,这意味着它正在使用 libmysqlclient 如果它是 mysqlnd,那么它正在使用本机驱动程序。

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

有关此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.)

(我知道这已经很老了,但是这些知识可以为我节省数小时的技术支持辩论,我首先看到了这一点。)