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
Release: 2024-11-26 05:24:09
Original
1000 people have browsed it

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

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

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!

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