Search code examples
phpmysqlmamp

MAMP: Undefined method mysqli_stmt::get_result()


I am relatively new to using PHP and MySQL and have run into trouble with the following code.

$stmt = $this->conn->prepare("SELECT * FROM users WHERE user_name = ?");
            $stmt->bind_param("s", $username);
            if ($stmt->execute()) {
                $user = $stmt->get_result()->fetch_assoc();
                $stmt->close();
                return $user;
            }

The error: Call to undefined method mysqli_stmt::get_result() is produced on the running of this script presumably due to MAMP not supporting mysqlnd. I have tried to work out how to install the native driver to MAMP but it does not look possible - (I have submitted a bug report and would suggest other MAMP users do so as well).

The line below seems to be the issue:

 $user = $stmt->get_result()->fetch_assoc();

Can anyone suggest an alternative method of producing the same outcome but without mysqlnd?

Many thanks.


Solution

  • Check your phpinfo(). I have mysqlnd, but only the PDO exension. You can tell by looking a the API Extensions under the mysqlnd section (if mysqlnd is installed).

    Instead of re-writing everything in PDO, this worked for me:

    $stmt = $this->conn->prepare("SELECT name, email, api_key, status, created_at FROM users WHERE email = ?");
        $stmt->bind_param("s", $email);
        if ($stmt->execute()) {
            $stmt->bind_result($name, $email, $api_key, $status, $created_at);
    
            /* fetch values */
            mysqli_stmt_fetch($stmt);
    
            /* set values */
            $user['name'] = $name;
            $user['email'] = $email;
            $user['api_key'] = $api_key;
            $user['created_at'] = $created_at;
    
            $stmt->close();
            return $user;
        } else {
            return NULL;
        }