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>
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!