2014-09-19 63 views
-1

嘗試訪問我所在的頁面時出現以下錯誤。MySQL/PHP - 嘗試使用數組時遇到致命錯誤?

Fatal error: Cannot use object of type mysqli_result as array in /.../comment.php on line 44

任何人都可以幫助什麼問題可能是什麼? 44號線是低於...

$posts = mysqli_query($sql, "SELECT * FROM posts WHERE postid = '$related'") or die($posts . "<br/>" . mysqli_error($sql)); 

$postsId = $posts['postid']; // <-- This is line 44 
+0

$ posts不是一個數組,這就是它告訴你的。你需要看看fetch_array – Hammerstein 2014-09-19 20:47:59

+0

這是Hammerstein的正確答案。你必須將其作爲答案發布。 – heximal 2014-09-19 20:49:02

+0

@heximal如果我沒有提供鏈接或更多信息,我不喜歡發佈這樣的內容。我並不擔心積分,更多的是關於幫助。 – Hammerstein 2014-09-19 20:51:02

回答

0

錯誤消息告訴你,你需要知道的一切:$posts包含您需要先使用mysqli_fetch_array(),例如得到實際數據的mysqli_result

+1

函數是mysqli_fetch_array。 – 2014-09-19 20:57:51

0

試試:

$posts = mysqli_query($sql, "SELECT * FROM posts WHERE postid = '$related'") or die($posts . "<br/>" . mysqli_error($sql)); 
$postData = mysqli_fetch_array($posts); 
$postsId = $postData['postid']; 
0

$postsmysqli_result類型。這不是一個數組。一個數組將描述一個表格行,但是你可能會用你的SQL查詢獲得多個表格行。因此,在你獲得數據數組之前,你必須循環它們。有一些可能性來做到這一點:

$posts = mysqli_query($sql, "SELECT * FROM posts WHERE postid = '$related'") or die($posts . "<br/>" . mysqli_error($sql));  

while ($row = $posts->fetch_array()) { 
    // is_array($row) === true 
    var_dump($row["postid"]); 
} 

// or 

while ($row = $posts->fetch_object()) { 
    // ($row instanceof stdClass) === true 
    var_dump($row->postid); 
} 
相關問題