警告:mysql_fetch_assoc()预计参数1是资源,鉴于空

警告:mysql_fetch_assoc()预计参数1是资源,鉴于空

问题描述:

我得到了这个非常奇怪的问题,警告: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

只需更改参数名称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>"; 
} 

正如Frayne说,改变$ commment_result至$ comment_result。在另一个说明中,停止使用mysql并开始使用mysqli。 MySQL正在被剥夺。