What is the best approach to reformat MySQL table output in PHP?

When displaying MySQL table output in PHP, it is often necessary to reformat the data to make it more visually appealing or user-friendly. One approach is to iterate through the query results and format them into an HTML table for easy viewing. This can be achieved by using PHP's foreach loop to loop through the rows and columns of the query result and then echoing out the data within the HTML table tags.

<?php
// Assume $result is the result of a MySQL query

echo "<table>";
echo "<tr><th>ID</th><th>Name</th><th>Email</th></tr>";

foreach ($result as $row) {
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['email'] . "</td>";
    echo "</tr>";
}

echo "</table>";
?>