Home >Database >Mysql Tutorial >How to Properly Store and Display Unicode Hindi Strings in PHP and MySQL?

How to Properly Store and Display Unicode Hindi Strings in PHP and MySQL?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-30 20:18:18106browse

How to Properly Store and Display Unicode Hindi Strings in PHP and MySQL?

Storing and Displaying Unicode Strings (हिन्दी) Using 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:

  1. Encode using "view source": Open the "view source" of the webpage where the text appears correctly, copy the character codes (सूर्योदय in this case) and then insert this encoded string into the database.
  2. Set NAMES and CHARACTER SET: Alternatively, you can modify the database connection settings in your PHP script to explicitly define the character set and collation. This ensures that data is stored and retrieved using the same encoding. Here's an example:
$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!

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