Home >Backend Development >PHP Tutorial >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:
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
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!