How can you ensure that PHP properly reads data from a MySQL database, especially when dealing with special characters like umlauts?

When dealing with special characters like umlauts in a MySQL database, it is important to ensure that PHP is properly configured to handle character encoding. One way to do this is by setting the character set to UTF-8 in both the MySQL connection and the PHP script. This will ensure that data is stored and retrieved correctly, without any issues related to special characters.

// Set the character set to UTF-8 in the MySQL connection
$mysqli = new mysqli("localhost", "username", "password", "database");
$mysqli->set_charset("utf8");

// Set the character set to UTF-8 in the PHP script
header('Content-Type: text/html; charset=utf-8');
mysqli_set_charset($mysqli, "utf8");

// Retrieve data from the database
$query = "SELECT * FROM table";
$result = $mysqli->query($query);

// Loop through the results
while ($row = $result->fetch_assoc()) {
    // Output the data
    echo $row['column_name'];
}