What are some common pitfalls to avoid when using While loops in PHP to display query results?

One common pitfall when using While loops in PHP to display query results is forgetting to fetch the data from the result set within the loop, resulting in an infinite loop or no data being displayed. To avoid this issue, make sure to fetch the data using functions like `mysqli_fetch_assoc()` or `mysqli_fetch_array()` within the While loop.

// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "dbname");

// Run a query
$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);

// Display the results using a While loop
while ($row = mysqli_fetch_assoc($result)) {
    echo $row['username'] . "<br>";
}

// Close the connection
mysqli_close($conn);