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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分