How can PHP be used to dynamically create a table from a database?

To dynamically create a table from a database using PHP, you can first establish a connection to the database, retrieve the data you want to display in the table, and then loop through the data to generate the table rows dynamically. Using HTML and PHP, you can echo out the table structure and populate it with the data fetched from the database.

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

// Retrieve data from the database
$query = "SELECT * FROM table_name";
$result = mysqli_query($connection, $query);

// Create the table structure
echo "<table>";
echo "<tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr>";

// Populate the table rows with data
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 the database connection
mysqli_close($connection);
?>