2015-08-26 51 views
2

上得到bind_result()的值上db_handler.php如何另一頁

public function getAllUserList($user_id) { 
    $stmt = $this->conn->prepare("SELECT list_id,name,date_created FROM list WHERE user_id = ?"); 
    $stmt->bind_param("i", $user_id); 
    $stmt->execute(); 
    $list = $stmt->bind_result($list_id,$name,$date_created); 
    $stmt->close(); 
    return $list; 
} 

index.php

$result = $db->getAllUserList($user_id); 
while ($result->fetch()) { 
      $tmp = array(); 
      $tmp["id"] = $list_id; 
      $tmp["list_name"] = $name; 
      $tmp["createdAt"] = $date_created; 
      array_push($response["lists"], $tmp); 
     } 

它給

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

以前我是用$stmt->get_result()方法致命錯誤而不是bind_result,但它開始拋出錯誤

PHP Fatal error: Call to undefined method mysqli_stmt::get_result().

如果我在dbhandler.php頁面上使用while ($result->fetch())它工作正常。但它給出了錯誤index.php

+0

試一下這條線'$ stmt-> close();'形成你的代碼! – Saty

+0

在評論$ stmt-> close()後仍然給出同樣的錯誤; – kabir

+0

bind_result()具有bool返回類型,而get_result具有mysqli_result返回類型,這就是爲什麼值可以分配給任何變量,但值不能存儲在bind_result中。這個問題必須有另一種方法。 – kabir

回答

4

您需要返回$stmt而不是$list並且不關閉語句(直到獲取所有結果之後)。

您還需要將bind_result從功能中取出,因爲變量的範圍有所限制。

public function getAllUserList($user_id) { 
    $stmt = $this->conn->prepare("SELECT list_id,name,date_created FROM list WHERE user_id = ?"); 
    $stmt->bind_param("i", $user_id); 
    $stmt->execute(); 
    return $stmt; 
} 

... 

$result = $db->getAllUserList($user_id); 

$result->bind_result($list_id,$name,$date_created); 

bind_result(等$list)返回一個布爾值,指示成功或失敗,而不是結果集,所以你要調用一個true(或falsefetch()

+0

超薄應用程序錯誤 由於以下錯誤的應用程序不能運行: 詳細 類型:ErrorException 碼:2 消息:mysqli_stmt ::取()[mysqli的-stmt.fetch]:無法取mysqli_stmt PHP致命錯誤:調用一個非對象的成員函數fetch() 發現新錯誤 – kabir

+0

感謝您的解決方案。你輕鬆解釋。再一次感謝你 – kabir