dstt1818
dstt1818
2014-05-26 13:10

MAMP:未定义的方法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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongyu3967 dongyu3967 7年前

    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;
        }
    
    点赞 评论 复制链接分享

相关推荐