Reputation: 1426
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?
This has really been bothering me lately. On another post @inspire answered correctly but doesn't work for me. You can find that here: How to know if MySQLnd is the active driver?
When i echo this:
<?php
$mysqlnd = function_exists('mysqli_fetch_all');
if ($mysqlnd) {
echo 'mysqlnd enabled!';
}
nothing happens at all, so apparently it's not enabled even though my phpinfo() says it's 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!';
}
When i try this i get call to member function getAttribute() on a non object..
Any help would be greatly appreciated. Thanks in advance.
Upvotes: 2
Views: 3034
Reputation: 9039
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, themysqli
extension and thePDO 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!';
}
Upvotes: 1
Reputation: 157880
There is no point in checking for the mysqlnd driver itself, either in phpinfo or in the code.
There is no point in checking for other API either. You have to check if desired API is based on mysqlnd, not other one. So, checking mysqli if you need PDO makes no sense.
To check PDO you have to have a PDO object first, as error message suggests.
Upvotes: 0