Displaying Unicode Data with PHP
To correctly display Unicode data in PHP, it's essential to set the appropriate character encoding. When PHP connects to a database, it may not handle Unicode data properly by default.
Example
Consider the following code:
<code class="php">$sql = mysql_query("select title from abd where tid='1'"); $row = mysql_fetch_array($sql); $title = $row['title']; echo $title;</code>
This code attempts to retrieve and display the title column from the abd table. However, if the column contains Unicode characters, it may not render correctly and display as question marks.
Solution
To resolve this issue, set the character encoding after establishing a connection to the database:
<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>
By setting the client character set, result character set, and connection collation to UTF-8, PHP will properly handle and display Unicode characters.
Updated Example
After adding the character encoding settings, the code will be:
<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'"); $sql = mysql_query("select title from abd where tid='1'"); $row = mysql_fetch_array($sql); $title = $row['title']; echo $title;</code>
This updated code will correctly display Unicode characters, resolving the issue of garbled output.
The above is the detailed content of How to Display Unicode Data Correctly in PHP?. For more information, please follow other related articles on the PHP Chinese website!

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!
