2016-04-28 79 views
-2

我得到了這個非常奇怪的問題,警告:mysql_fetch_assoc()預計參數1是資源,鑑於空

Warning: mysql_fetch_row() expects parameter 1 to be resource, null given ... 

不過,我檢查mysql_num_rows($結果),這是不爲0

echo '<h3>Comments</h3>'; 
    $comment_query = "select * from comments where post_id='$post_id' order by comment_timestamp desc"; 
    $comment_result = mysql_query($comment_query) or die(mysql_error()); 
    if (mysql_num_rows($comment_result) != 0) { 
     while ($crow = mysql_fetch_assoc($commment_result)) { 
     $comment_username = $crow['username']; 
     $comment_text = $crow['comment_text']; 
     $comment_time = $crow['comment_timestamp']; 
     echo "<b>$comment_username </b><b>$comment_timestamp: </b> <br />".$comment_text."<hr /><br>"; 
     } 
    } else { echo "No comments yet!"; } 

有關於此的任何想法?我一直堅持一段時間,非常感謝您的幫助!

+3

'commment_result'在'mysql_fetch_assoc'中有一個額外的'm'。 –

+0

[mysql \ _fetch \ _array()/ mysql \ _fetch \ _assoc()/ mysql \ _fetch \ _row()的可能重複需要參數1爲資源或mysqli \ _result,布爾值給定](http://stackoverflow.com/questions/2973202/mysql-fetch-array-mysql-fetch-assoc-mysql-fetch-row-expect-parameter-1-to) – aldrin27

+1

謝謝@FrayneKonok,我真的有一個愚蠢的問題...... – Vicky

回答

2

只需更改參數名稱mysql_fetch_assoc。這是錯誤的/不正確的。

變化$commment_result變爲$comment_result

while ($crow = mysql_fetch_assoc($comment_result)) { 
    $comment_username = $crow['username']; 
    $comment_text = $crow['comment_text']; 
    $comment_time = $crow['comment_timestamp']; 
    echo "<b>$comment_username </b><b>$comment_timestamp: </b> <br />".$comment_text."<hr /><br>"; 
} 
0

正如Frayne說,改變$ commment_result至$ comment_result。在另一個說明中,停止使用mysql並開始使用mysqli。 MySQL正在被剝奪。

相關問題