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

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
}
?>

USE YOUR COUNTER VARIABLE AS FLAG TO CHECK IF LOOP EXECUTED

just append following

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

Before for loop use below code. if($result->rowCount() > 0){ Here use your for loop }else{ here you can use anything like error message (jquery/javascript/html/php) }