在mysql_fetch_array中使用if语句

时间:2013-03-30 00:55:53

标签: php mysql if-statement

我有以下查询。如果成人人数少于10,我想显示时间。

$result = mysql_query("SELECT sum(Adult) AS Number1, Time FROM orders WHERE confirmed = 'Y' and BookDate = '2013-04-01' GROUP BY Time");

while($row = mysql_fetch_array($result)) {

if ($row['Number1'] < '10')
echo '<div class="result">'.$row['Time'].' ('.$row['Number1'].')</div>';

}

所以它会像这样......

<div class="result">18:00:00 (8)</div>
<div class="result">18:30:00 (5)</div>
<div class="result">19:00:00 (6)</div>

但是我也想表明18:15:00有0个成年人。 我试过添加一些这样的if语句

if (($row['Time'] == '18:15:00') and ($row['Number1'] < '10')) {
echo '<div class="result">'.$row['Time'].' ('.$row['Number1'].')</div>';
}
else {
echo '<div class="result">18:15:00 (0)</div>';
}

但它不断循环。有人能告诉我哪里出错了吗?

3 个答案:

答案 0 :(得分:0)

如果你想从循环语句中break,你可以使用break语句:

while(condition){
    if(conditionMet){
        break;
    }
}

答案 1 :(得分:0)

我不确定你要做什么,但也许就是这样:

if ($row['Number1'] < '10') {
  echo '<div class="result">'.$row['Time'].' ('.$row['Number1'].')</div>';
} elseif ($row['Time']) == '18:15:00') {
  echo '<div class="result">18:15:00 (0)</div>';
}

答案 2 :(得分:0)

变化: -

if (($row['Time'] == '18:15:00') and ($row['Number1'] < '10')) 
{
echo '<div class="result">'.$row['Time'].' ('.$row['Number1'].')</div>';
}
else 
{
echo '<div class="result">18:15:00 (0)</div>';
}

if ($row['Number1'] < '10') 
{
echo '<div class="result">'.$row['Time'].' ('.$row['Number1'].')</div>';
}
elseif(($row['Time']) == '18:15:00') 
{
echo '<div class="result">18:15:00 (0)</div>';
}