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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器