Home > Database > Mysql Tutorial > body text

How to Display Unicode Data Correctly in PHP When Retrieving from MySQL?

Susan Sarandon
Release: 2024-10-30 09:20:27
Original
176 people have browsed it

How to Display Unicode Data Correctly in PHP When Retrieving from MySQL?

Unicode Data Display in PHP

To display Unicode data in PHP, the data must be stored and transmitted using a Unicode character encoding, such as UTF-8. When retrieving Unicode data from a MySQL database, it's important to ensure that the database connection and query are configured properly for Unicode support.

Issue:

The provided MySQL query retrieves Unicode data but displays question marks instead of the expected characters. This indicates an encoding mismatch between the data and the output.

Solution:

To resolve this issue and display Unicode characters correctly, add the following lines to your PHP code after establishing the MySQL connection:

<code class="php">mysql_query ("set character_set_client='utf8'");
mysql_query ("set character_set_results='utf8'");
mysql_query ("set collation_connection='utf8_general_ci'");</code>
Copy after login

These lines set the character set and collation for the client, the results, and the connection, respectively, to ensure that Unicode data is handled properly.

After making these changes, the PHP code should display Unicode characters as intended.

The above is the detailed content of How to Display Unicode Data Correctly in PHP When Retrieving from MySQL?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!