What are common pitfalls when outputting query results in PHP?

One common pitfall when outputting query results in PHP is not properly handling errors or empty result sets. To avoid this, always check if the query was successful and if there are results to display before outputting them.

// Example of handling errors and empty result sets when outputting query results

// Execute the query
$result = mysqli_query($connection, "SELECT * FROM users");

// Check if the query was successful
if ($result) {
    // Check if there are results to display
    if (mysqli_num_rows($result) > 0) {
        // Output the results
        while ($row = mysqli_fetch_assoc($result)) {
            echo $row['username'] . "<br>";
        }
    } else {
        echo "No results found.";
    }
} else {
    echo "Error: " . mysqli_error($connection);
}