1

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);
  • probably a fatal error? nothing to see on screen ./ source of browser? Is error reporting ON? `` – Ivo P Jul 05 '17 at 00:45

1 Answers1

1

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.