Storage of Unicode Data in Hindi Language in MySQL
In a PHP application utilizing MySQL, it's essential to know how to store and retrieve Hindi language data effectively. Let's delve into the process of storing data in MySQL in a readable Unicode format.
To store Hindi data in a readable format, the utf8 character set and utf8_general_ci collation should be employed. Ensure that the collation of the field designated for Hindi text storage is set to utf8_general_ci. To rectify any existing field, utilize the following code:
ALTER TABLE `<table_name>` CHANGE `<field_name>` `<field_name>` VARCHAR(100) CHARSET utf8 COLLATE utf8_general_ci DEFAULT '' NOT NULL;
Upon database connection, begin with this statement:
mysql_set_charset('utf8');
For instance:
// Character set configuration mysql_set_charset('utf8'); // Insert Hindi text mysql_query("INSERT INTO ....");
To retrieve Hindi data, the charset configuration should also be employed:
// Character set configuration mysql_set_charset('utf8'); // Select Hindi text mysql_query("SELECT * FROM ....");
Prior to displaying Unicode text (Hindi in this case) on a browser, it's imperative to specify the content type with a meta tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Code Example:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />Example Unicode
Note:
The preferred method for changing the character set is mysql_set_charset('utf8'). Utilizing mysql_query() (e.g., SET NAMES utf8) is discouraged.
The above is the detailed content of How to Store and Retrieve Hindi Language Data in MySQL Using PHP?. For more information, please follow other related articles on the PHP Chinese website!