这里我如何调用我的查询,我想使用简单的if ... else语句,但我在某个地方犯了错误,有人可以解释我在哪里以及为什么?
$result = mysqli_query($con, $sql) or die(mysqli_error($con,$sql));
echo mysqli_error($con);
$number=mysqli_num_rows($result);
while ($row= mysqli_fetch_array($result)){
$brand = $row['brand'];
$model = $row['model'];
$reg_num = $row['reg_num'];
$horse_powers = $row['horse_powers'];
$color = $row['color'];
echo $number;
if($number<1){
echo "No rented cars for this period !";
}
else{
echo $brand;
}
}
答案 0 :(得分:1)
请尝试以下代码:
$number = 0;
$result = mysqli_query($con, $sql);
if (!$result)
exit(mysqli_error($con));
else
$number = mysqli_num_rows($result);
if ($number==0) {
echo "No rented cars for this period !";
} else {
while ($number){
$row = mysqli_fetch_row($result);
$brand = $row['brand'];
$model = $row['model'];
$reg_num = $row['reg_num'];
$horse_powers = $row['horse_powers'];
$color = $row['color'];
echo $brand;
$number--;
}
}
答案 1 :(得分:0)
此if语句的输出始终为false。
if($number<1){
echo "No rented cars for this period !";
}
else{
echo $brand;
}
如果$number < 1
,则不会运行while循环。因此,如果$number < 1
它将永远不会到达if语句。