Storing Hindi Unicode Data in MySQL and PHP
When working with multilingual data, it's crucial to handle character encoding correctly. This article addresses the question of storing Hindi text in MySQL using PHP in a readable format.
How to Store Hindi Unicode in MySQL
Choose the Right Character Set and Collation:
Select 'utf8' character set and 'utf8_general_ci' collation for the database and table field where you intend to store Hindi text. This ensures proper storage and comparison of Unicode characters.
Alter Existing Fields:
If you have an existing table and need to modify the field to store Hindi, run the following SQL statement:
ALTER TABLE `<table_name>` CHANGE `<field_name>` `<field_name>` VARCHAR(100) CHARSET utf8 COLLATE utf8_general_ci DEFAULT '' NOT NULL;
PHP Configuration for Hindi Unicode Handling
Set Character Set:
Always set the character set to 'utf8' to ensure that PHP handles Unicode correctly. Do this with:
mysql_set_charset('utf8');
Execute this before database interaction for both inserting and retrieving Hindi text.
Encoding Retrieved Data:
After retrieving Hindi text from MySQL, ensure that you send the appropriate HTTP header to indicate UTF-8 encoding to the browser:
header('Content-Type: text/html; charset=utf-8');
HTML Meta Tag:
To ensure that Hindi text is displayed correctly in a web browser, add a meta tag to the HTML document's
section:<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Additional Notes:
By following these steps, you can effectively store and retrieve Hindi Unicode text in MySQL using PHP, ensuring accurate and readable display of Hindi content.
The above is the detailed content of How to Store and Retrieve Hindi Unicode Data in MySQL Using PHP?. For more information, please follow other related articles on the PHP Chinese website!