Skip to content
PHP Logo

What are some best practices for outputting query results in PHP, especially for complex queries involving multiple tables?

When outputting query results in PHP, especially for complex queries involving multiple tables, it is best practice to use a loop to iterate through the results and display them in a structured format. This can help organize the data and make it easier to read and understand for the end user.

php
<?php
// Assume $queryResult contains the result of a complex query involving multiple tables

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