Until the loop displays all the values

I have such a strange problem. There are seven admins: Dart, Jane, Luke, Najin, Root, Sam and Sydney.

CODE:

    <table>
          <tr>
            <th style="text-align: left; width: 200px;">Username</th>
            <th colspan="2" style="text-align: left;">Action</th>
          </tr>
        <?php 

            $sql = "SELECT * FROM admins ORDER BY Admin_Username ASC";
            $result = mysqli_query($connection, $sql);
            $admin = mysqli_fetch_assoc($result);

            while($admin = mysqli_fetch_assoc($result)) { 
            ?>
          <tr>
            <td><?php echo ($admin["Admin_Username"]); ?></td>
            <td><a href="edit_admin.php?id=<?php echo urlencode($admin["id"]); ?>">Edit</a></td>
            <td><a href="delete_admin.php?id=<?php echo urlencode($admin["id"]); ?>" onclick="return confirm('Are you sure you want to delete this admin?');">Delete</a></td>
          </tr>
        <?php     
} 
?>
</table>

If I use ASCorder, the first admin, darth does not appear in the loop, and if I use order DESC, the last administrator, sydney does not appear. What could be the problem here?

+4
source share
5 answers

Get rid of the first line $admin =.

Your loop will select all of them; you don’t need to extract the first one separately (and if this happens, skipping it because your loop immediately extracts the second before the first can be written out).

+4
source

delete this line

 $admin = mysqli_fetch_assoc($result);//You already fetching the first result here
+3
source

$admin = mysqli_fetch_assoc($result); while, . , .

+2
    <?php 

            $sql = "SELECT * FROM admins ORDER BY Admin_Username ASC";
            $result = mysqli_query($connection, $sql);    
            while($admin = mysqli_fetch_assoc($result)) { 
            ?>
          <tr>
            <td><?php echo ($admin["Admin_Username"]); ?></td>
            <td><a href="edit_admin.php?id=<?php echo urlencode($admin["id"]); ?>">Edit</a></td>
            <td><a href="delete_admin.php?id=<?php echo urlencode($admin["id"]); ?>" onclick="return confirm('Are you sure you want to delete this admin?');">Delete</a></td>
          </tr>
    <?php     
    } 
    ?>
+2
$admin = mysqli_fetch_assoc($result);

while. , / ,

0
source

All Articles