IF / ELSE没有显示所需的结果

时间:2012-10-23 07:27:39

标签: php

在此查询中查找注释,如果有任何注释应该显示,如果没有任何注释它应该显示没有注释。

评论显示正确,但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>";
    }
}

2 个答案:

答案 0 :(得分: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>";
    }
}

答案 1 :(得分:0)

问题是:

if(mysql_num_rows($comment) < 0 )

您不能少于0行。改为:

if(mysql_num_rows($comment) <= 0 )

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