如果/当我的查询没有返回任何结果时,如何检测到该结果,以便我可以显示错误消息?
表会议室:
id name no_of_rooms
1 Lake View 4
2 Royale 2
3 Sky View 6
表预订:
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
我的Html表单:
<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>
我的PHP代码:
<?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
}
?>
答案 0 :(得分:0)
使用您的计数器变量作为标记来检查是否已执行循环
只需追加
<?php
if($i==0)
echo "No record fetched";
?>
答案 1 :(得分:0)
在代码之前使用循环之前。 if($ result-&gt; rowCount()&gt; 0){ 这里使用你的for循环 }其他{ 在这里你可以使用任何像错误消息(jquery / javascript / html / php) }