值为零时if语句不工作(PHP)

值为零时if语句不工作(PHP),php,if-statement,Php,If Statement,如果我将If($comments\u count==0)更改为If($comments\u count==1),它会回显文本(如果有1条注释)。但是回到==0,该命令不会执行。我尝试在一个没有注释的页面上回显$comments\u count的值,结果显示为0。但是if-else忽略了它,并且没有打印任何内容 代码如下: $query = "SELECT * FROM comments WHERE id = {$set_id}"; $all_comments_set = mysql_qu

如果我将
If($comments\u count==0)
更改为
If($comments\u count==1)
,它会回显文本(如果有1条注释)。但是回到
==0
,该命令不会执行。我尝试在一个没有注释的页面上回显$comments\u count的值,结果显示为0。但是if-else忽略了它,并且没有打印任何内容

代码如下:

$query = "SELECT * FROM comments WHERE id = {$set_id}";
    $all_comments_set = mysql_query($query); 
    $comments_count = mysql_num_rows($all_comments_set);

    while($comment = mysql_fetch_array($all_comments_set)) {
        if($comments_count == 0) {
            echo $comments_count;
            echo "<div class='comments'><p>There are no comments to show right now. Be the first to comment!</p></div>";
        } else {
            echo $comments_count;   
            echo "<div class='comments'>
                <p class='commenter'><a href=''>".$comment['commentAuthor']."</a></p>
                <p>".$comment['comment']."</p>
              </div>";
        }
    }
$query=“从注释中选择*,其中id={$set\u id}”;
$all\u comments\u set=mysql\u query($query);
$comments\u count=mysql\u num\u行($all\u comments\u set);
while($comment=mysql\u fetch\u数组($all\u comments\u set)){
如果($comments\u count==0){
echo$comments\u count;
echo“现在没有要显示的评论。请首先发表评论!

”; }否则{ echo$comments\u count; 回声“

“$comment['comment']”

"; } }
您需要将if语句移出while循环。由于行数为0,mysql_fetch_数组调用将不会返回结果,内部while循环代码也不会执行

if($comments_count == 0) {
        echo $comments_count;
        echo "<div class='comments'><p>There are no comments to show right now. Be the first to comment!</p></div>";
} else {
    while(....){
    }
}
if($comments\u count==0){
echo$comments\u count;
echo“现在没有要显示的评论。请首先发表评论!

”; }否则{ 而(……){ } }

作为补充说明,如果可以的话,您真的应该切换到使用语句,或者至少使用
mysql\u real\u escape\u string
对输入进行转义,以防止SQL注入攻击。

您需要将if语句移出while循环。由于行数为0,mysql_fetch_数组调用将不会返回结果,内部while循环代码也不会执行

if($comments_count == 0) {
        echo $comments_count;
        echo "<div class='comments'><p>There are no comments to show right now. Be the first to comment!</p></div>";
} else {
    while(....){
    }
}
if($comments\u count==0){
echo$comments\u count;
echo“现在没有要显示的评论。请首先发表评论!

”; }否则{ 而(……){ } }

作为补充说明,如果可以的话,您真的应该切换到使用语句,或者至少使用
mysql\u real\u escape\u string
来转义您的输入,以防止SQL注入攻击。

可能是因为如果没有行,mysql\u fetch\u数组将返回false。

可能是因为如果没有行,mysql\u fetch\u数组将返回falsefalse。

如果您已经知道自己没有注释,那么您肯定不想遍历该
while
循环?如果您已经知道自己没有注释,那么您肯定不想遍历该
while
循环?