What is the best practice for outputting MySQL table data in PHP to display in a table format with multiple columns?

When outputting MySQL table data in PHP to display in a table format with multiple columns, it is best practice to fetch the data from the database using a query, loop through the results, and output each row as a table row with each column value as a table data cell. This can be achieved by using HTML markup within PHP to generate the table structure.

<?php
// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Fetch data from MySQL table
$sql = "SELECT * FROM your_table";
$result = $conn->query($sql);

// Output data in table format
echo "<table>";
echo "<tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr>";

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "<tr>";
        echo "<td>" . $row['column1'] . "</td>";
        echo "<td>" . $row['column2'] . "</td>";
        echo "<td>" . $row['column3'] . "</td>";
        echo "</tr>";
    }
} else {
    echo "0 results";
}

echo "</table>";

// Close database connection
$conn->close();
?>