What are common pitfalls when trying to display database results in a table using PHP?

One common pitfall when trying to display database results in a table using PHP is not properly handling empty result sets. To solve this, you can check if the result set is empty before attempting to display it in a table.

// Assume $result contains the database query result

if(mysqli_num_rows($result) > 0) {
    echo "<table>";
    while($row = mysqli_fetch_assoc($result)) {
        echo "<tr>";
        foreach($row as $value) {
            echo "<td>".$value."</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "No results found.";
}