What are some common issues with UTF-8 encoding and PHP when retrieving data from a database?
Common issues with UTF-8 encoding and PHP when retrieving data from a database include displaying garbled or incorrectly encoded characters. To solve this issue, you can set the connection character set to UTF-8 before querying the database.
// Set the connection character set to UTF-8
$mysqli = new mysqli("localhost", "username", "password", "database");
$mysqli->set_charset("utf8");
// Retrieve data from the database
$result = $mysqli->query("SELECT * FROM table");
// Loop through the results
while ($row = $result->fetch_assoc()) {
// Output the data
echo $row['column_name'];
}
// Close the connection
$mysqli->close();