Home >Database >Mysql Tutorial >How Can I Correctly Convert Latin1 Characters in a UTF8 MySQL Table to UTF8?

How Can I Correctly Convert Latin1 Characters in a UTF8 MySQL Table to UTF8?

DDD
DDDOriginal
2024-12-09 04:29:09780browse

How Can I Correctly Convert Latin1 Characters in a UTF8 MySQL Table to UTF8?

Convert Latin1 Characters in a UTF8 Table to UTF8

You've encountered an issue where characters with diacritics (e.g., "Jáuò Iñe") were incorrectly stored in a UTF8 table due to a missing "mysql_set_charset('utf8')" call.

To resolve this, you're attempting to convert the affected rows using "mb_convert_encoding" and "iconv." However, these methods are failing to capture characters beyond the first "illegal" character (represented as "ă" in the example).

The recommended approach to correct the data is to use the MySQL function:

convert(cast(convert(name using  latin1) as binary) using utf8)

Explanation:

  1. The outer convert function converts the data from its current binary representation to a string using the UTF-8 character set.
  2. The inner convert function converts the data from its original Latin-1 representation to a binary representation.
  3. The cast function ensures that the inner conversion returns a binary value.

In some cases, the inner conversion may not be necessary, as the data may already be stored in a binary format suitable for conversion to UTF-8. Testing both options should determine the correct approach for your situation.

The above is the detailed content of How Can I Correctly Convert Latin1 Characters in a UTF8 MySQL Table to UTF8?. 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