Home > Database > Mysql Tutorial > How to Store and Display Hindi Data Correctly in MySQL and PHP?

How to Store and Display Hindi Data Correctly in MySQL and PHP?

Patricia Arquette
Release: 2024-11-11 01:20:02
Original
672 people have browsed it

How to Store and Display Hindi Data Correctly in MySQL and PHP?

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:

  1. Use utf8_encode() to convert the data into a Unicode format.
  2. Insert the converted data into the MySQL field assigned as utf8 character set and utf8_general_ci collation.

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');
Copy after login

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" />
Copy after login

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'];
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template