0

我计算每个帖子的评论数。它有效:

$numComments = mysql_query("SELECT COUNT(id_post) FROM comments WHERE id_post = '". $row["id"]."' ");  
// it works if I do this: echo mysql_result($numComents,0);

但我更喜欢仅在有任何评论时才显示评论数。但我有条件的问题。

if ($numComments > 0){
    echo mysql_result($numComments,0); 
}else{

}

我怎么能说如果有评论显示数字。如果有 0 条评论不显示任何内容(我不希望它显示“有 0 条评论”)

4

2 回答 2

2

尝试:

$result = mysql_query("SELECT COUNT(id_post) FROM comments WHERE id_post = '". $row["id"]."' "); 
$numComments = mysql_result($result,0);
if ( $numComments  > 0) {
 echo $numComments ; 
 } else {
   //do something...
 }
于 2012-11-13T14:34:28.823 回答
1

尝试:

$number_of_rows = mysql_fetch_row($numComments)[0];

echo $number_of_rows > 0 ? $number_of_rows : ''; 
于 2012-11-13T14:28:46.943 回答