How can PHP be used to correctly display database content with special characters like umlauts?
Special characters like umlauts can be correctly displayed in PHP by ensuring that the database connection is set to the correct character set, such as UTF-8. This can be done by executing a query to set the character set before retrieving data from the database. Additionally, using htmlentities() or htmlspecialchars() functions can help encode special characters to prevent any potential issues with displaying them.
// Set the character set for the database connection
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase;charset=utf8", "username", "password");
// Execute query to set the character set
$pdo->exec("set names utf8");
// Retrieve data from the database
$stmt = $pdo->query("SELECT * FROM mytable");
// Display data with special characters
while ($row = $stmt->fetch()) {
echo htmlentities($row['column_name'], ENT_QUOTES, 'UTF-8');
}