-2

在这个查询中寻找评论,如果有评论应该显示,如果没有评论应该显示No comments。

评论显示正确,但显示没有任何评论的 if 函数不起作用。

$comments = mysql_query ("SELECT * FROM comments WHERE post_id = '". $_GET['id']."'");

while ($comment = mysql_fetch_array ($comments)) {

    //If there are'nt any results
    if(mysql_num_rows($comment) < 0 ) {
        echo "No comments yet!";

} else {

    //If there are any results
        echo "<p>" . $comment['comment'] . "</p>
            <p><b>" . $comment['author'] . "</b>, " . date("M j, Y ", strtotime($comment["date"])) . "</p>";
    }
}
4

2 回答 2

1

您应该重新组织您的代码,并且在比较 0 条评论时出现错误:

$comments = mysql_query ("SELECT * FROM comments WHERE post_id = '". $_GET['id']."'");

//If there are'nt any results
if(mysql_num_rows($comments) == 0 ) { // <-- watch for 0 results
    echo "No comments yet!";            
}
else {
  while ($comment = mysql_fetch_array ($comments)) {                        
    //If there are any results
    echo "<p>" . $comment['comment'] . "</p>
    <p><b>" . $comment['author'] . "</b>, " . date("M j, Y ", strtotime($comment["date"])) . "</p>";
    }
}
于 2012-10-23T07:30:08.493 回答
0

问题是:

if(mysql_num_rows($comment) < 0 )

您不能少于 0 行。改成:

if(mysql_num_rows($comment) <= 0 )

(或== 0,或者当它为0但不高于0时会触发的任何东西)

于 2012-10-23T07:32:26.820 回答