Organic
Organic

Reputation: 255

PHP 7 Uncaught Error: Call to undefined method mysqli_stmt::get_result() with mysqlnd installed

I get this error on my live webserver but not while testing on localhost. So it's unlikely that the code is the culprit.

I understand that there are workarounds for this problem but I'd like to understand why I am experiencing it in PHP 7.0.18 with mysqlnd module installed.

enter image description here

This is my DBController class. The error occurs when I call the query() function.

class DBController
{ 
private $db;

private $servername = "localhost";
private $username = "hidden";
private $password = "hidden";
private $database = "hidden";

function __construct(){
    $this->db = new mysqli($this->servername, $this->username, $this->password);
    $this->db->select_db($this->database);
}

function db(){
    return $this->db;
}

private function query($sql){
    $sql = $this->db->prepare($sql);
    $sql->execute();
    return $sql->get_result();
}

function close(){
    $this->db->close();
}
}

Upvotes: 1

Views: 1105

Answers (2)

Organic
Organic

Reputation: 255

It turns out that PHP 7 was misconfigured (and therefore not properly supporting MySQLi) by my host provider. They have since moved me to a new server which works. Thanks all for your help.

Upvotes: 0

axiac
axiac

Reputation: 72256

Everything is explained in the first paragraph of the Overview of MySQL Native Driver documentation page on php.net:

What it is not

Although MySQL Native Driver is written as a PHP extension, it is important to note that it does not provide a new API to the PHP programmer. The programmer APIs for MySQL database connectivity are provided by the MySQL extension, mysqli and PDO MYSQL. These extensions can now use the services of MySQL Native Driver to communicate with the MySQL Server. Therefore, you should not think of MySQL Native Driver as an API.

In plain English, MySQL Native Driver (mysqlnd) is not enough to use MySQL in PHP code. You also need one of the extensions that provide an API: MySQLi or PDO MySQL.

The code you posted reveals that you are using MySQLi to work with MySQL. It requires to also install/enable the mysqli extension to work.

Upvotes: 1

Related Questions