How can the code snippet be optimized for better performance or readability?

The code snippet can be optimized for better performance by using a single SQL query to fetch all the data at once instead of making multiple queries in a loop. This will reduce the number of database calls and improve the overall performance of the code.

// Fetch all user data in a single query
$query = "SELECT * FROM users";
$result = mysqli_query($connection, $query);

// Check if the query was successful
if ($result) {
    // Loop through the results and process each row
    while ($row = mysqli_fetch_assoc($result)) {
        // Process the user data
        echo "User ID: " . $row['id'] . ", Username: " . $row['username'] . "<br>";
    }
} else {
    // Handle query error
    echo "Error: " . mysqli_error($connection);
}

// Free the result set
mysqli_free_result($result);