Home  >  Article  >  Database  >  How to Display Unicode Data Correctly in PHP: Why Am I Seeing Gibberish Instead of My Tamil

How to Display Unicode Data Correctly in PHP: Why Am I Seeing Gibberish Instead of My Tamil

Patricia Arquette
Patricia ArquetteOriginal
2024-10-30 07:46:27472browse

How to Display Unicode Data Correctly in PHP: Why Am I Seeing Gibberish Instead of My Tamil

How to Display Unicode Data Correctly in PHP

When displaying Unicode data in PHP, it's important to ensure proper encoding to preserve special characters. Let's consider a case:

A table called 'abc' contains two rows with Unicode titles:

tid    title
1      வெள்ளிக்கிழமை ஐ.
2      கோலாகல தொடக்க

When executing the query $sql=mysql_query("select title from abd where tid='1'"), the result may display gibberish instead of the actual Unicode characters. This is because the encoding is not properly set.

Solution:

To resolve this issue, adjust the character encoding 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>

This sets the encoding for client, results, and connection to UTF-8. UTF-8 is a Unicode character encoding that can correctly represent the vast majority of Unicode characters.

Once the encoding is set, re-execute the query to fetch and display the Unicode title correctly:

<code class="php">    $sql=mysql_query("select title from abd where tid='1'");
    $row=mysql_fetch_array($sql);
    $title = $row['title'];
    echo $title;</code>

With these settings, the Unicode characters will now be displayed as intended:

வெள்ளிக்கிழமை ஐ.

The above is the detailed content of How to Display Unicode Data Correctly in PHP: Why Am I Seeing Gibberish Instead of My Tamil. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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