致命错误:在非对象上调用成员函数fetch_assoc()


Fatal error: Call to a member function fetch_assoc() on a non-object

如何修复此错误?这是我的密码。

$stmt = $this->conn->prepare("SELECT ngno, email, encrypted_password, name, user_type FROM `guide` WHERE email = ?");
    $stmt->bind_param("s", $email);
    if ($stmt->execute()) {
        $user = $stmt->bind_result($ngno, $email, $encrypted_password, $name, $user_type)->fetch_assoc();
        $stmt->close();

我使用的是远程SQL数据库。我也在本地主机上尝试过这个。但后来我得到了对布尔上的成员函数fetch_assoc()的错误、致命错误调用

此代码使用get_result而不是bind_result。但我需要使用bind_result。

我已将代码更改为以下内容,

$stmt = $this->conn->prepare("SELECT ngno, email, encrypted_password, name, user_type FROM `guide` WHERE email = ?");
    $stmt->bind_param("s", $email);
        
    if ($stmt->execute()) {
        $stmt->bind_result($ngno, $email, $encrypted_password, $name, $user_type);
        $user = $stmt->fetch_assoc();
        $stmt->close();
        if($encrypted_password == $password){
            return $user;
        }
    } else {
        return NULL;
    }
    

我现在收到这个错误。

致命错误:调用未定义的方法mysqli_stmt::fetch_assoc()

我的查询失败了吗?

bind_result()不返回对象,而是返回状态。这意味着它不能链接到来自该对象的另一个方法调用。你需要在两条独立的线上完成。

有关详细信息,请参阅bind_result手册页面。

正如Julie Pelletier上面提到的,您不能链接bind_result,因为它不返回对象。试试这个:

$stmt = $this->conn->prepare("SELECT ngno, email, encrypted_password, name, user_type FROM `guide` WHERE email = ?");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
    $stmt->bind_result($ngno, $email, $encrypted_password, $name, $user_type);
    $user = $stmt->fetch();
    $stmt->close();

注意:使用mysqli_stmt_execute()时,在执行任何其他查询之前,必须使用mysqli_stmt_fetch()函数来获取数据