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

How to Fix Double-Encoded UTF8 Characters in MySQL?

DDD
Release: 2024-10-30 23:22:30
Original
740 people have browsed it

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)
Copy after login

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);
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template