Home >Database >Mysql Tutorial >How Can I Convert Latin1 Characters to UTF-8 in a UTF-8 Database Table?

How Can I Convert Latin1 Characters to UTF-8 in a UTF-8 Database Table?

Susan Sarandon
Susan SarandonOriginal
2024-12-02 21:40:12702browse

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:

  • The convert() function first casts the current name column value to Latin1 encoding using the latin1 converter.
  • The resulting binary string is then cast back to a string using the binary converter.
  • Finally, the string is converted to UTF-8 using the utf8 converter.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn