Home >Database >Mysql Tutorial >How Can I Convert Latin1 Characters to UTF-8 in a UTF-8 Database Table?
Converting Latin1 Characters to UTF8 in a UTF8 Table
Your question describes an issue where previously inserted data containing diacritics was corrupted due to incorrect charset handling.
Problem:
Your PHP scripts did not specify the UTF-8 charset for database communication, leading to Latin1 characters being stored in a UTF-8 table. This resulted in the characters being displayed incorrectly as "Ã" sequences.
Solution:
The solution lies in using a MySQL function called convert() to explicitly cast the data from Latin1 encoding to UTF-8. Here's an example query:
UPDATE `table` SET `name` = convert(cast(convert(`name` using latin1) as binary) using utf8)
Explanation:
This conversion process ensures that any corrupted Latin1 characters are properly converted to UTF-8, preserving the correct display of diacritics. Note that the inner conversion using the convert() function may not be necessary in all cases, depending on the specific data corruption.
The above is the detailed content of How Can I Convert Latin1 Characters to UTF-8 in a UTF-8 Database Table?. For more information, please follow other related articles on the PHP Chinese website!