PHP / MySQL简单的IF / ELSE并不那么简单

时间:2012-12-26 12:58:10

标签: php mysql

我认为我的思绪一定要经历一个节礼日的混乱。

我正在为运动队的每场比赛建立基本评论部分。

因此,当没有输入任何评论时(在MySQL数据库中),我只想显示“成为第一个输入评论”;否则,以html格式显示评论结果表。

我可以轻松显示评论结果表。

出于某种原因,我无法获得IF没有评论正常工作。现在感觉非常业余。 。 。 : - )

我已宣布行数:

 $row_count = 0;

我在while语句中添加了计数

     while($row = mysql_fetch_array($result))
     {
          // adding to count
          $row_count++;

我的计数正常,因为我可以在屏幕上显示行号。

这是IF / ELSE我的代码:

    if ($row_count === 0) {
        echo "<p>Be the first to enter a game comment and earn points toward your next fan badge.</p>";
    } else {
                   // no need to show code as this already works!

4 个答案:

答案 0 :(得分:2)

你可以使用

mysql_num_rows($queryReference)

希望这会有所帮助。 感谢。

答案 1 :(得分:1)

请使用以下功能打印此值并告诉我输出是什么

var_dump($row_count);

或者您可以使用==而不是===

答案 2 :(得分:1)

$query = mysql_query("SELECT * FROM comments");
$c = mysql_num_rows($query);
if($c==0) {
    echo "<p>Be the first to enter a game comment and earn points toward your next fan badge.</p>";
}
else {
    while($row = mysql_fetch_array($query))
    {
        $vars = $row[index];
    }
}

答案 3 :(得分:0)

$ row_count = 0;

我在while语句中添加了计数

     while($row = mysql_fetch_array($result))
     {
          // adding to count
          $row_count++;
       }

我的计数正常,因为我可以在屏幕上显示行号。

这是IF / ELSE代码:

    if ($row_count == 0) {
        echo "<p>Be the first to enter a game comment and earn points toward your next fan badge.</p>";
    } else {
                   // no need to show code as this already works!
             }