2016-05-05 111 views
1

我该如何解决此错误?这是我的代码。致命错误:调用非对象的成员函数fetch_assoc()

$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。任何帮助,将不胜感激。

编辑1:

我已经改变了代码以下,

$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()

我的查询失败了吗?

+0

检查是否设置了'$ email' .. –

回答

1

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

查看bind_result manual page了解详情。

0

正如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()功能必须用来获取之前执行任何额外的查询的数据。

+0

感谢您的回复。我试过这个。现在我得到一个错误,调用未定义的方法mysqli_stmt :: fetch_assoc()。我的查询失败了吗?我测试了电子邮件和密码与isset。值是正确的。 –

相关问题