Home  >  Article  >  Database  >  How to Store and Retrieve Hindi Unicode Data in MySQL Using PHP?

How to Store and Retrieve Hindi Unicode Data in MySQL Using PHP?

DDD
DDDOriginal
2024-11-11 13:53:03575browse

How to Store and Retrieve Hindi Unicode Data in MySQL Using PHP?

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

  1. 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.

  2. 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

  1. 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.

  2. 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');
  3. 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:

  • For PHP versions 8.1 and later, mysql_set_charset() is deprecated. Use mysqli_set_charset() instead.
  • When debugging, check that your editor and terminal are configured for UTF-8 encoding to avoid display issues.

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!

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