What is the correct syntax for querying a database table in PHP and displaying the results in an HTML table?

When querying a database table in PHP and displaying the results in an HTML table, you need to establish a database connection, execute the query, fetch the results, and then loop through the results to display them in an HTML table. This involves using PHP functions such as mysqli_connect, mysqli_query, mysqli_fetch_assoc, and looping through the results with a foreach loop.

<?php
// Establish a connection to the database
$conn = mysqli_connect("localhost", "username", "password", "database");

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Execute a query to select data from a table
$sql = "SELECT * FROM table_name";
$result = mysqli_query($conn, $sql);

// Display results in an HTML table
echo "<table>";
while ($row = mysqli_fetch_assoc($result)) {
    echo "<tr>";
    foreach ($row as $value) {
        echo "<td>" . $value . "</td>";
    }
    echo "</tr>";
}
echo "</table>";

// Close the connection
mysqli_close($conn);
?>