What are the best practices for handling multiple results in a SELECT query and displaying them in a tabular format in PHP?
When handling multiple results in a SELECT query in PHP, it is best practice to fetch the results using a loop and display them in a tabular format using HTML. This can be achieved by looping through the results array and echoing out the data within HTML table tags.
<?php
// Assuming $results is an array of results fetched from a SELECT query
echo "<table>";
echo "<tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr>";
foreach ($results as $row) {
echo "<tr>";
echo "<td>" . $row['column1'] . "</td>";
echo "<td>" . $row['column2'] . "</td>";
echo "<td>" . $row['column3'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
Keywords
Related Questions
- How can using TRUE or FALSE in PHP comparisons affect the outcome of the code execution?
- What is the significance of using the extract() function in PHP, and how can it impact the readability and functionality of the code?
- What are some common patterns or approaches in PHP projects where classes may not have explicit method calls in the index.php file?