How can PHP be used to dynamically generate HTML content based on database entries?

To dynamically generate HTML content based on database entries using PHP, you can establish a connection to the database, query the necessary data, and then loop through the results to output HTML elements accordingly. This allows for the creation of dynamic web pages that display content retrieved from a database.

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

// Query database for entries
$query = "SELECT * FROM table_name";
$result = mysqli_query($connection, $query);

// Loop through results and generate HTML content
while($row = mysqli_fetch_assoc($result)) {
    echo "<div>";
    echo "<h2>" . $row['title'] . "</h2>";
    echo "<p>" . $row['content'] . "</p>";
    echo "</div>";
}

// Close database connection
mysqli_close($connection);
?>