Home >Database >Mysql Tutorial >How to Fix Double-Encoded UTF8 Characters in MySQL?

How to Fix Double-Encoded UTF8 Characters in MySQL?

DDD
DDDOriginal
2024-10-30 23:22:30732browse

How to Fix Double-Encoded UTF8 Characters in MySQL?

Recovering from Double-Encoded UTF8 Characters

Data previously imported using LOAD DATA INFILE was mistakenly assumed to be encoded in Latin1. Consequently, multibyte characters were split into individual bytes and subsequently encoded in UTF8, resulting in double-encoding. This leads to corrupted characters such as "ñ" instead of "ñ".

To rectify these errors, a special MySQL function can be utilized to restore the correct UTF8 strings after double-encoding:

CONVERT(CAST(CONVERT(field USING latin1) AS BINARY) USING utf8)

This function can be integrated into an UPDATE statement to correct the affected fields:

UPDATE tablename SET
    field = CONVERT(CAST(CONVERT(field USING latin1) AS BINARY) USING utf8);

By executing this statement, the database will replace the double-encoded values with their correct UTF8 counterparts, resolving the character anomalies.

The above is the detailed content of How to Fix Double-Encoded UTF8 Characters in MySQL?. 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