dongzai5181 2017-07-05 00:27
浏览 93
已采纳

由于PHP代码,localhost不加载

my xampp localhost was working well till i add this code to my php file

<?php while ($notification) {?>
<li>
<?php 
  echo $notification['notification'];
  ?>
  </li>
  <?php
} ?>

now the page is not loading or partially loading

here $notification is

$notification_sql= "SELECT id FROM notifications WHERE user_id='{$_SESSION['user']}'";

       $notification_query = mysqli_query($conn, $notification_sql);
      $notification = mysqli_fetch_assoc($notification_query);
  • 写回答

1条回答 默认 最新

  • dora0817 2017-07-05 01:20
    关注

    Before I begin I want to recommend you something: Avoid the use of the while statetements. Unless they are really needed - like in the exact way they are used in the PHP docs (and even then you can find alternatives) - they should be avoided all the time. I present the motive down under.

    That said,... it's not $notification['notification'], but $notification['id'].

    After you change it, you still remain with the issue: an infinite loop. Because you are using a while loop without changing the state of the loop condition. E.g_ you are validating the $notification array for beeing existent. Because it exists all the time - it's state never beeing changed in the loop - then the iterations will be infinite in number. In order to avoid this dangerous (!!!) situation, you can use the following codes.

    Method 1:

    Notice the difference: $notification is valid only for the period of a loop step. After each iteration $notification is newly created. And, when mysqli_fetch_assoc() returns FALSE on the (last + 1)-iteration step, then the $notification receives that value and, therefore, the loop ends.

    <?php
    $notification_sql = "SELECT id FROM notifications WHERE user_id='{$_SESSION['user']}'";
    
    $notification_query = mysqli_query($conn, $notification_sql);
    
    if ($notification_query) {
        while ($notification = mysqli_fetch_assoc($notification_query)) {
            ?>
            <li>
                <?php
                echo $notification['id'];
                ?>
            </li>
            <?php
        }
    
        mysqli_free_result($notification_query);
    }
    

    ?>

    Method 2:

    Or, if you want to fetch the results in an array and to output its items later, then you can do it like this:

    <?php
    $notification_sql = "SELECT id FROM notifications WHERE user_id='{$_SESSION['user']}'";
    
    $notification_query = mysqli_query($conn, $notification_sql);
    
    $notifications = array();
    
    if ($notification_query) {
        while ($row = mysqli_fetch_assoc($notification_query)) {
            $notifications[] = $row['id'];
        }
    
        mysqli_free_result($notification_query);
    }
    
    // OTHER STUFF AFTER FETCHING...
    ?>
    
    <?php
    // LOOPING AT A LATER TIME.
    foreach ($notifications as $notificationId) {
        ?>
        <li>
            <?php
            echo $notificationId;
            ?>
        </li>
        <?php
    }
    ?>
    

    Other recommendations:

    • Use prepared statements in order to avoid MySQL injection.
    • Use exception handling in order to catch all errors and handle them correspondingly. Especially when you run database operations.
    • Use PDO instead of mysqli.

    Here I have provided full code examples of prepared statements combined with exception handling (using mysqli library):

    Good luck.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下