How can PHP be used to query a MySQL database in UTF-8 and generate HTML5 with UTF-8 encoding?

When querying a MySQL database in UTF-8 and generating HTML5 with UTF-8 encoding in PHP, it is important to ensure that the connection to the database is set to use UTF-8 encoding and that the HTML content type is also set to UTF-8. This can be achieved by specifying the charset in the connection settings and setting the Content-Type header to UTF-8 in the PHP script.

<?php
// Connect to MySQL database with UTF-8 encoding
$mysqli = new mysqli("localhost", "username", "password", "database");
$mysqli->set_charset("utf8");

// Set UTF-8 encoding for HTML content
header('Content-Type: text/html; charset=utf-8');

// Query the database and generate HTML content
$result = $mysqli->query("SELECT * FROM table");
echo "<html>";
echo "<head><meta charset='UTF-8'></head>";
echo "<body>";
while($row = $result->fetch_assoc()) {
    echo "<p>" . $row['column'] . "</p>";
}
echo "</body>";
echo "</html>";

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