'PHP: I have failed to select all data i inserted in the table although, some one help me

I inserted the data in the table very well and it's showing in phpMyadmin but when i try to display data from the database only one single item is displayed. I need help.

Code and screenshoots below

<?php 
                
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
    $count = mysqli_num_rows($result);
        if($count > 0){
            while($row = mysqli_fetch_assoc($result)){
               $incidentId = $row['id'];
               $icTypeName = $row['inctype_name'];
               $addedBy = $row['added_by'];
               $addedOn = $row['added_on'];
            }

?>
            <tr class="tableData">
              <td><?php echo $incidentId;?></td>
              <td><?php echo $icTypeName;?></td>
              <td><?php echo $addedBy;?></td>
              <td><?php echo $addedOn;?></td>
              <td><i class="uil uil-edit icon editIcon"></i></td>
            </tr>

<?php
           } else {
             $_SESSION['noData'] = 'No incidents to show!';
           }
        } else {
          die('Failed to Connect!');
        }       
?>

Images below

enter image description hereenter image description here

I expect to get all the data in the database tables displayed with the "SELECT * FROM nw"



Solution 1:[1]

Simply move the } that ends the while loop to after the code that is using the variables. You are currently consuming all the resultset before outputting anything, so you will only see the last rows data.

<?php 
                
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
    $count = mysqli_num_rows($result);
        if($count > 0){
            while($row = mysqli_fetch_assoc($result)){
               $incidentId = $row['id'];
               $icTypeName = $row['inctype_name'];
               $addedBy = $row['added_by'];
               $addedOn = $row['added_on'];
            //}   REMOVED

?>
            <tr class="tableData">
              <td><?php echo $incidentId;?></td>
              <td><?php echo $icTypeName;?></td>
              <td><?php echo $addedBy;?></td>
              <td><?php echo $addedOn;?></td>
              <td><i class="uil uil-edit icon editIcon"></i></td>
            </tr>

<?php
            } // END OF WHILE LOOP
        } else {
            $_SESSION['noData'] = 'No incidents to show!';
        }
    } else {
        die('Failed to Connect!');
    }       
?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RiggsFolly