如果MySQL语句返回空结果,则重定向到另一个页面

am working on reports and currently have a problem with how the output should be when and if the MySQL statement returns an empty result. i have two problems i.e 1) i need to use the WHILE statement to give all possible results when query is true. 2) i think an ELSE statement needs to be used to redirect me to another page if the result is false. Now i have noticed that WHILE and ELSE statements cannot be used together in that way. correct me if am wrong. again if am to use IF where there is WHILE, the output will be limited to only one record.

            <html>
            <head>
            <title>Messages Received</title>
            </head>
            <body>
                  <h1 align="center">Messeges Received</h1>
                  <form id="form1" method="post" action="">
                    <div align="center">
                    <table width="700" border="1" >
                      <tr style="background:#093; color:#FFF; font-weight:bold">
                        <td width="150">Name</td>
                        <td width="180">Email/User_ID</td>
                        <td width="174">Subject</td>
                        <td width="168">Date Received</td>
                        </tr>


                          <?php  
            include("connect.php");
            mysql_select_db("ceec", $con);

            $query = "SELECT * FROM messeges ORDER BY date DESC";

            $result=mysql_query($query);

            while ($row=mysql_fetch_array($result)){

                        $id=$row['id'];
                        $date = date("D d M, Y", strtotime($row['date']));
                echo '
                 <tr>
                    <td>'. $row["name"] .'</td>
                    <td>'. $row["email"] .'</td>
                    <td>'. $row["sub"] .'</td>
                    <td>'. $date .'</td>
                  </tr>';
            }
            else
            {
            header("Location: no_sms.php");
            }   

            ?>
             </table>

            </form>
           </body>
           </html>
<?php
$query = "SELECT * FROM messeges ORDER BY date DESC";

$result = mysql_query($query);

if (!empty($result)) {

    while ($row = mysql_fetch_array($result)) {

        $id = $row['id'];
        $date = date("D d M, Y", strtotime($row['date']));

        echo '
             <tr>
                <td>' . $row["name"] . '</td>
                <td>' . $row["email"] . '</td>
                <td>' . $row["sub"] . '</td>
                <td>' . $date . '</td>
              </tr>';
    }
    //while end
} //if end 

else {
    header("Location: no_sms.php");
}

?>

Try this its working :

     <html>
                <head>
                <title>Messages Received</title>
                </head>
                <body>
                      <h1 align="center">Messeges Received</h1>
                      <form id="form1" method="post" action="">
                        <div align="center">
                        <table width="700" border="1" >
                          <tr style="background:#093; color:#FFF; font-weight:bold">
                            <td width="150">Name</td>
                            <td width="180">Email/User_ID</td>
                            <td width="174">Subject</td>
                            <td width="168">Date Received</td>
                            </tr>


                              <?php  
                include("connect.php");
                mysql_select_db("ceec", $con);

                $query = "SELECT * FROM messeges ORDER BY date DESC";

                $result=mysql_query($query);

              $row = mysql_fetch_array($result);

       if(!empty($row)) {
         do {
                    echo '
                     <tr>
                        <td>'. $row["name"] .'</td>
                        <td>'. $row["email"] .'</td>
                        <td>'. $row["sub"] .'</td>
                        <td>'. $date .'</td>
                      </tr>';
                }while($row=mysql_fetch_array($result));
}
                else
                {
                header("Location: no_sms.php");
                }   

                ?>
                 </table>

                </form>
               </body>
               </html>
$result=mysql_query($query);

//This Will Direct Redirect If Result Set Is Empty

if(empty($result)){

  header("Location: no_sms.php");

}

while ($row=mysql_fetch_array($result)){

  $id=$row['id'];

  $date = date("D d M, Y", strtotime($row['date']));

  echo '
  <tr>
   <td>'. $row["name"] .'</td>
    <td>'. $row["email"] .'</td>
     <td>'. $row["sub"] .'</td>
     <td>'. $date .'</td>
    </tr>';
}