Home >Backend Development >PHP Tutorial >How to Fix Broken UTF-8 Encoding in MySQL and PHP?

How to Fix Broken UTF-8 Encoding in MySQL and PHP?

DDD
DDDOriginal
2024-11-26 05:24:09977browse

How to Fix Broken UTF-8 Encoding in MySQL and PHP?

Fixing Broken UTF-8 Encoding

When dealing with UTF-8 encoding, it's common to encounter broken characters like î due to incorrect handling. This issue can arise from database configuration, PHP settings, or even text editor encoding.

MySQL Character Set and PHP Header

Ensure that your MySQL database is using a UTF-8 collation like utf8_general_ci. Additionally, verify that your PHP code includes a proper UTF-8 header, such as:

<?php
header("Content-Type: text/html; charset=utf-8");
?>

Checking Notepad and phpMyAdmin

Confirm that Notepad is configured to use UTF-8 without BOM. In phpMyAdmin, ensure that character encoding is set to UTF-8 for both data display and SQL export.

Identifying Broken Accented Characters

While not all accented characters may be affected, common broken sequences include î, í, and ü. These characters represent accented versions of "e," "i," and "u," respectively.

Solution: Double Encoding Fix

If you suspect double-encoded UTF-8 characters, consider using the following procedure:

  1. Dump your MySQL data using:
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
  1. Reload the data into a UTF-8 character set:
mysql -h DB_HOST -u DB_USER -p DB_PASSWORD \
--default-character-set=utf8 DB_NAME < DB_NAME-dump.sql

This process forcibly converts double-encoded characters to valid UTF-8.

Source:
[Fixing Double Encoded UTF-8 Data in MySQL](http://blog.hno3.org/2010/04/22/fixing-double-encoded-utf-8-data-in-mysql/)

The above is the detailed content of How to Fix Broken UTF-8 Encoding in MySQL and PHP?. 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