What are some best practices for assigning a database query result to a specific element in an HTML table using PHP?

When assigning a database query result to a specific element in an HTML table using PHP, it is important to loop through the query result and dynamically populate the table rows with the data. One common approach is to use a foreach loop to iterate over the query result and generate the table rows with the corresponding data.

<?php
// Assuming $queryResult is the result of your database query

echo "<table>";
echo "<tr><th>Column 1</th><th>Column 2</th></tr>";

foreach ($queryResult as $row) {
    echo "<tr>";
    echo "<td>" . $row['column1'] . "</td>";
    echo "<td>" . $row['column2'] . "</td>";
    echo "</tr>";
}

echo "</table>";
?>