Home >Database >Mysql Tutorial >How Can I Fix Broken UTF-8 Encoding in My PHP and MySQL Database?
Fixing Broken UTF-8 Encoding
When working with UTF-8 encoding in PHP and MySQL, you may encounter instances where characters are displayed incorrectly as î, ÃÂ, ü, and others like it. This can be due to improper handling of the encoding.
To resolve this issue, you can employ a solution that involves temporarily changing the character set of your MySQL database to latin1, dumping the data, and then reading it back into the database with the correct UTF-8 character set.
To do so, follow these steps:
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
mysql -h DB_HOST -u DB_USER -p DB_PASSWORD \ --default-character-set=utf8 DB_NAME < DB_NAME-dump.sql
This process temporarily changes the database character set, reads the dumped data using a character set that guarantees no double-encoding, and re-inserts it with the correct UTF-8 encoding. This approach provides a simple and effective way to fix broken UTF-8 encoding.
The above is the detailed content of How Can I Fix Broken UTF-8 Encoding in My PHP and MySQL Database?. For more information, please follow other related articles on the PHP Chinese website!