2

当我尝试使用 getAttribute 函数时,出现此错误:调用非对象上的成员函数 getAttribute()。似乎 mysqlnd 已启用,但我也不能使用 get_result(),知道吗?

最近这个问题真的很困扰我。在另一篇帖子中,@inspire 回答正确,但对我不起作用。你可以在这里找到:How to know if MySQLnd is the active driver?

当我回应这个:

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

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

什么都没有发生,所以即使我的 phpinfo() 说它已启用,它显然也没有启用?

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

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

当我尝试这个时,我会调用非对象上的成员函数 getAttribute()..

任何帮助将不胜感激。提前致谢。

4

2 回答 2

1

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 with MySQLnd. Other clients inside PHP, such as the older mysql or the MySQL PDO driver may still be using mysqld.

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

<?php
$hasMySQL = false;
$hasMySQLi = false;
$withMySQLnd = false;

if (function_exists('mysql_connect')) {
    $hasMySQL = true;
    $sentence.= "(Deprecated) MySQL <b>is installed</b> ";
} else 
    $sentence.= "(Deprecated) MySQL <b>is not</b> installed ";

if (function_exists('mysqli_connect')) {
    $hasMySQLi = true;
    $sentence.= "and the new (improved) MySQL <b>is installed</b>. ";
} else
    $sentence.= "and the new (improved) MySQL <b>is not installed</b>. ";

if (function_exists('mysqli_fetch_all')) {
    $withMySQLnd = true;
    $sentence.= "This server is using MySQLnd as the driver.";
} else
    $sentence.= "This server is using libmysqlclient as the driver.";

echo $sentence;

This works because mysqlnd provides three additional functions that work only when mysqlnd is used as the driver. mysqli_fetch_all is one of these functions, however, it is best to not only check for a function that only shows if mysqli has been compilied with mysqlnd, but also a function that shows if mysqli is compiled at all.

This will allow you to present better error messages to the client.

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

$db = new PDO('mysql:host=localhost;dbname=<SOMEDB>', '<USERNAME>', 'PASSWORD');
if (strpos($pdo->getAttribute(PDO::ATTR_CLIENT_VERSION), 'mysqlnd') !== false) {
    echo 'PDO MySQLnd enabled!';
}
于 2014-03-19T07:11:36.547 回答
0

在 phpinfo 或代码中检查 mysqlnd 驱动程序本身是没有意义的。

检查其他 API 也没有意义。您必须检查所需的 API是否基于 mysqlnd,而不是其他的。因此,如果您需要 PDO,则检查 mysqli 是没有意义的。

要检查 PDO,您必须首先拥有一个 PDO 对象,如错误消息所示

于 2014-03-19T08:37:53.560 回答