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

时间:2014-10-15 09:41:53

标签: php mysql sql

正在研究报告,目前在MySQL语句返回空结果的时候输出应该是怎么回事。我有两个问题,即1)当查询为真时,我需要使用WHILE语句给出所有可能的结果。 2)我认为如果结果为假,则需要使用ELSE语句将我重定向到另一个页面。现在我注意到WHILE和ELSE语句不能以这种方式一起使用。如果我错了,请纠正我。再次,如果我使用IF有WHILE,输出将仅限于一个记录。

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

3 个答案:

答案 0 :(得分:1)

<?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");
}

?>

答案 1 :(得分:1)

$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>';
}

答案 2 :(得分:0)

尝试使用它:

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