关闭
dongzai5181 2017-07-04 16: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-04 17: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.

    展开全部

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部