"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > 在UTF8 MySQL表中正确将Latin1字符转换为UTF8的方法

在UTF8 MySQL表中正确将Latin1字符转换为UTF8的方法

2025-06-16에 게시되었습니다
검색:240

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.

최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3