Home > Database > Mysql Tutorial > How to Repair Corrupted UTF-8 Encoding in MySQL Databases?

How to Repair Corrupted UTF-8 Encoding in MySQL Databases?

Patricia Arquette
Release: 2024-12-07 07:58:13
Original
576 people have browsed it

How to Repair Corrupted UTF-8 Encoding in MySQL Databases?

Repairing Corrupted UTF-8 Encoding

This article addresses the issue of memperbaiki pengkodean UTF-8 yang rakus in MySQL databases. The problem manifests as garbled characters like "î" when PHP 5 is used with a utf8_general_ci collation and a proper UTF-8 header is set.

Mapping of Corrupted Characters

The following function can be utilized to map instances of corrupted characters to their corresponding correct UTF-8 characters:

function fixUtf8($string) {
  $replacements = array(
    "î" => "®",
    "í" => "í",
    "ü" => "ü"
  );
  
  return str_ireplace(array_keys($replacements), array_values($replacements), $string);
}
Copy after login

Database Dump and Restoration

Another approach involves dumping the database data and reloading it to resolve double-encoded UTF-8 characters:

mysqldump -h DB_HOST -u DB_USER -p DB_PASSWORD --opt --quote-names \
    --skip-set-charset --default-character-set=latin1 DB_NAME > DB_NAME-dump.sql

mysql -h DB_HOST -u DB_USER -p DB_PASSWORD \
    --default-character-set=utf8 DB_NAME < DB_NAME-dump.sql
Copy after login

This method typically resolves the issue effectively.

The above is the detailed content of How to Repair Corrupted UTF-8 Encoding in MySQL Databases?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template