What are common issues with character encoding in PHP when retrieving data from a database?
Common issues with character encoding in PHP when retrieving data from a database include mismatched character sets between the database and PHP, leading to garbled or incorrectly displayed text. To solve this issue, ensure that the character sets are consistent across the database, PHP, and the HTML output. Use functions like `utf8_encode()` or `utf8_decode()` to handle any encoding mismatches.
// Set the character encoding for PHP
mysqli_set_charset($conn, 'utf8');
// Retrieve data from the database
$query = "SELECT * FROM table";
$result = mysqli_query($conn, $query);
// Loop through the results and encode the text
while($row = mysqli_fetch_assoc($result)) {
$encodedText = utf8_encode($row['column_name']);
echo $encodedText;
}