What are the potential pitfalls of converting database entries from ISO-8859-1 to UTF-8 in PHP?

Converting database entries from ISO-8859-1 to UTF-8 in PHP can lead to data corruption if not done correctly. One potential pitfall is that characters outside the ASCII range may not be converted properly, resulting in mojibake or question mark characters. To avoid this, make sure to properly set the connection encoding, convert the data using the iconv or mb_convert_encoding functions, and update the database schema to use UTF-8.

// Set the connection encoding to UTF-8
mysqli_set_charset($connection, 'utf8');

// Fetch the data from the database
$result = mysqli_query($connection, "SELECT * FROM table");
while ($row = mysqli_fetch_assoc($result)) {
    // Convert the data from ISO-8859-1 to UTF-8
    foreach ($row as $key => $value) {
        $row[$key] = mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1');
    }
    
    // Update the database with the converted data
    // mysqli_query($connection, "UPDATE table SET column1 = '{$row['column1']}', column2 = '{$row['column2']}' WHERE id = {$row['id']}");
}