Unicode Data Display in PHP
When dealing with Unicode data in PHP, it's common to encounter issues with improper display. One such case is the inability to render Unicode characters correctly.
Issue:
The provided code attempts to display Unicode data from a MySQL database, but it shows incorrect characters as question marks.
Solution:
To resolve this, it's necessary to set the character encoding for both the client and results in MySQL. This ensures that PHP can properly decode and display the Unicode characters. Here's the modified code:
<code class="php">mysql_query("set character_set_client='utf8'"); mysql_query("set character_set_results='utf8'");</code>
This sets the client and result character sets to UTF-8, allowing PHP to correctly interpret and display Unicode data.
Original Issue:
The original code did not specify the character encoding, which resulted in incorrect display of Unicode characters. By setting the encoding explicitly, PHP can handle the data appropriately.
Additional Considerations:
Depending on your database configuration, you may also need to set the collation to utf8_general_ci to ensure proper handling of Unicode data. This can be achieved with the following query:
<code class="php">mysql_query("set collation_connection='utf8_general_ci'");</code>
These adjustments ensure that PHP can display Unicode data accurately.
The above is the detailed content of Why do Unicode characters display as question marks in PHP and how to fix it?. For more information, please follow other related articles on the PHP Chinese website!