2016-05-05 115 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。值是正確的。 –

相關問題