Home > Database > Mysql Tutorial > body text

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

Patricia Arquette
Release: 2024-10-30 07:46:27
Original
471 people have browsed it

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      கோலாகல தொடக்க
Copy after login

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>
Copy after login

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>
Copy after login

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

வெள்ளிக்கிழமை ஐ.
Copy after login

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!

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!