IT gIrLy GiRl
IT gIrLy GiRl

Reputation: 337

To show "no such result found" in while loop?

<?php
$s=mysql_query("SELECT * FROM bus_detail WHERE source_point='$source_point' AND destination_point='$destination' AND `".$day."` = 'yes'");
while($row=mysql_fetch_array($s)) {
    $count+=1;
    ?>
    <tr class="td_text">
    <td><?php echo $count; ?></td>
    <td><input name="bus_name" type="text" value="<?php echo $row['bus_name'];?>" class="input_box" /></td>
    <td><?php echo $row['bus_no'];?></td>
    <td><input name="bus_id" type="text" value="<?php echo $row['bus_id'];?>" style="border:0px; width:15px; text-align:left;" /></td>
    <td><?php echo $row['departure_time'];?></td>
    <td><?php echo $row['arrival_time'];?></td>
    <td><?php echo $row['duration'];?></td>
    <td><?php echo $row['seat_availablity'];?></td>
    <td><?php echo $row['price'];?></td>
    <td><a href="book.php?bus_id=<?php echo $row['bus_id'];?>"><input type="submit" value="Book Now" style="border:0px;" class="book" title="Search here" /></a></td></tr>
    <?php
}
?>

This code is totaly alright. But if there is no result according to the query I want to show "No such result found". I tried while(!$s), but no result. Any help?

I want to show the result in the blank field enter image description here

Upvotes: 1

Views: 247

Answers (4)

Elaine Ong
Elaine Ong

Reputation: 9

Detect the rows count as suggested above, then keep the no result string in a variable.

$num_rows = mysql_num_rows($s);
if($num_rows == 0) {
  $string_no_result = "No such result found";
  return;
}

After the TR and TD for the heading bar, print the no result string.

 echo $string_no_result;

Upvotes: 0

IT gIrLy GiRl
IT gIrLy GiRl

Reputation: 337

enter image description here

showing like this .. cant i put the result after the heading bar ???

Upvotes: 3

MaGnetas
MaGnetas

Reputation: 5108

You can use mysql_num_rows to check how many rows have been returned:

if (mysql_num_rows($s) > 0)
{
    while($row=mysql_fetch_array($s)) {
        $count+=1;
    ?>
        <tr class="td_text">
        <td><?php echo $count; ?></td>
        <td><input name="bus_name" type="text" value="<?php echo $row['bus_name'];?>" class="input_box" /></td>
        <td><?php echo $row['bus_no'];?></td>
        <td><input name="bus_id" type="text" value="<?php echo $row['bus_id'];?>" style="border:0px; width:15px; text-align:left;" /></td>
        <td><?php echo $row['departure_time'];?></td>
        <td><?php echo $row['arrival_time'];?></td>
        <td><?php echo $row['duration'];?></td>
        <td><?php echo $row['seat_availablity'];?></td>
        <td><?php echo $row['price'];?></td>
        <td><a href="book.php?bus_id=<?php echo $row['bus_id'];?>"><input type="submit" value="Book Now" style="border:0px;" class="book" title="Search here" /></a></td></tr>
    <?php
    }
}
else
   echo "No such result found";

Upvotes: 0

Rumi
Rumi

Reputation: 229

You can use mysql_num_rows($s), this will return the number of rows the query fetched, if there is no result, number of rows returned will be 0.

Add below line before executing the while loop,

$num_rows = mysql_num_rows($s);
if($num_rows == 0) {
  echo "No such result found";
  return;
}

Upvotes: 2

Related Questions