douweibiao8471 2014-10-15 09:41
浏览 36

如果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>
  • 写回答

3条回答 默认 最新

  • dongyu1918 2014-10-15 09:48
    关注
    <?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");
    }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类