Home >Database >Mysql Tutorial >How to Properly Store and Display Unicode Hindi Strings in PHP and MySQL?
When working with unicode strings, it's crucial to ensure proper handling at all stages, including storage in a database, retrieval, and display on webpages. This article addresses the issue faced by a developer who encountered difficulties displaying Hindi text in a PHP script sourced from a MySQL database.
The initial setup involved creating a database with UTF-8 encoding and utf8_bin collation, adding a varchar field with UTF-8 charset, and attempting to insert the Hindi text "सूर्योदय:05:30" directly. However, upon fetching and displaying the data using echo(utf8_encode($string)), the browser returned "?????."
The reason for this behavior lies in the character encoding mismatch. The text "सूर्योदय" should be stored as UTF-8, but it was copied directly from a source where it was likely encoded in a different format. To resolve this, two methods can be employed:
$result = mysql_query("SET NAMES utf8"); $cmd = "select * from hindi"; $result = mysql_query($cmd);
By using either of these methods, the Hindi text can be stored correctly in the database and rendered properly on a webpage.
Regarding the question of a script to convert Hindi text to HTML character codes, you can search for online tools or libraries that provide this functionality. Additionally, HTML Character Code websites (e.g., https://www.w3schools.com/charsets/ref_html_utf8.asp) can be used to obtain the character codes for specific Unicode characters.
It's important to note that when displaying Hindi characters on a webpage, it's essential to specify the character set in the HTML header or using a PHP header command to ensure that the browser interprets the text correctly.
The above is the detailed content of How to Properly Store and Display Unicode Hindi Strings in PHP and MySQL?. For more information, please follow other related articles on the PHP Chinese website!