What is the best way to output MySQL query results in different columns using PHP?
When outputting MySQL query results in different columns using PHP, you can fetch the data from the database and then loop through the results to display them in separate columns. One way to achieve this is by using HTML table structure with separate table cells for each column.
<?php
// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
// Perform a query
$query = "SELECT column1, column2, column3 FROM table";
$result = mysqli_query($connection, $query);
// Output data in columns
echo "<table>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['column1'] . "</td>";
echo "<td>" . $row['column2'] . "</td>";
echo "<td>" . $row['column3'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Close connection
mysqli_close($connection);
?>