Effective Unicode Storage for Hindi Data in MySQL and PHP
When working with an application using PHP and MySQL, ensuring that Hindi data is stored and displayed correctly can be crucial. Here's a detailed guide to address the following concerns:
Storing Hindi Data in MySQL
To store Hindi text in MySQL, you must use the appropriate character set and collation. The recommended combination is utf8 character set and utf8_general_ci collation. This allows for proper storage and retrieval of Hindi characters.
Converting and Storing Data
When users enter data in a textbox, they may enter it in a format different from the desired readable format. To convert and store the data correctly in MySQL, you can use the following steps:
Retrieving and Displaying Unicode Data
To retrieve and display Hindi text from MySQL, ensure the PHP connection is set to the utf8 character set:
mysql_set_charset('utf8');
Furthermore, HTML pages displaying Hindi text should include this meta tag to specify the content's character set:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Example Code
Here's an example code that demonstrates the steps discussed above:
// Set character set mysql_set_charset('utf8'); // Convert user input to Unicode $hindiText = utf8_encode($_POST['hindi_input']); // Store data in MySQL $query = "INSERT INTO hindi_table (hindi_data) VALUES ('$hindiText')"; mysql_query($query); // Retrieve and display data $result = mysql_query("SELECT * FROM hindi_table"); $row = mysql_fetch_assoc($result); echo $row['hindi_data'];
Conclusion
By following these steps, you can effectively store and retrieve Hindi data in MySQL using PHP, ensuring that the data is readable and displayed correctly.
The above is the detailed content of How to Store and Display Hindi Data Correctly in MySQL and PHP?. For more information, please follow other related articles on the PHP Chinese website!