doumo2501 2015-09-24 07:57
浏览 44

如果查询没有返回结果,如何显示错误消息?

If/when my query returns no results, how can I detect that, so that I can show an error message?

Table rooms:

id   name        no_of_rooms
1    Lake View        4
2    Royale           2
3    Sky View         6

Table bookings:

id    room_id  start       end          total_rooms
1       1      2015-09-25  2015-09-25        4
2       2      2015-09-25  2015-09-25        2
3       3      2015-09-25  2015-09-25        6

My Html form:

<form action="index1.php" method="get">
From : <input type="text" name="d1" class="tcal" value="" />
To: <input type="text" name="d2" class="tcal" value="" />
<input type="submit" value="Search"/>
</form>

My PHP code:

<?php
include('connect.php');
if (isset($_GET["d1"])) { $d1  = $_GET["d1"]; } else { $d1=0; }; 
if (isset($_GET["d2"])) { $d2  = $_GET["d2"]; } else { $d2=0; };
$result = $db->prepare("SELECT r.id AS roomID, r.name, r.no_of_rooms,
          r.no_of_rooms - COALESCE(t.bookedRooms,0) AS available_rooms
          FROM rooms AS r
LEFT JOIN (
SELECT room_id, SUM(total_rooms) AS bookedRooms
FROM bookings
WHERE `start` < :b AND `end` > :a
GROUP BY room_id ) AS t
ON r.id = t.room_id
WHERE r.no_of_rooms - COALESCE(t.bookedRooms,0) > 0 ");

$result->bindParam(':a', $d1);
$result->bindParam(':b', $d2);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){

<form method="get" action="form2.php">
 Name :<input type="text" name="d2" class="tcal" value="<?php echo $row['name']; ?>" />
available_rooms : <input type="text" name="id" class="tcal" value="<?php echo $row['available_rooms']; ?>" />
Select rooms:<input type="text" name="select" class="tcal" value="" />
<input type="submit" value="SELECT">
</form>
<?php
}
?>
  • 写回答

2条回答 默认 最新

  • dpz90118 2015-09-24 08:01
    关注

    USE YOUR COUNTER VARIABLE AS FLAG TO CHECK IF LOOP EXECUTED

    just append following

    <?php
    if($i==0)
    echo "No record fetched";
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大