How to Store and Display Unicode Strings in Hindi Using PHP and MySQL?

Patricia Arquette
Release: 2024-10-22 21:05:03
Original
164 people have browsed it

How to Store and Display Unicode Strings in Hindi Using PHP and MySQL?

Storing and Displaying Unicode String (हिन्दी) Using PHP and MySQL

You may encounter challenges when attempting to store and display Unicode strings, particularly in languages like Hindi, due to encoding issues. To address these concerns, it's essential to set the appropriate character encoding and collation in both your database and the PHP script.

Database Configuration

  • For the MySQL database, set the database and table encoding to UTF-8 and use the utf8_bin collation to ensure proper handling of Unicode characters.
  • In the table itself, ensure that the text field accepts UTF-8 text by setting the charset property to 'utf8'.

PHP Script

  • To display the Unicode string correctly, you must set the Content-Type header as follows:
<code class="php">header('Content-Type: text/html; charset=utf-8');</code>
Copy after login
  • Additionally, you can specify the character set for MySQL queries by setting NAMES utf8 before executing any query that retrieves data. For instance:
<code class="php">$result = mysql_query("SET NAMES utf8");</code>
Copy after login

Sample Script

The following PHP script demonstrates how to store and retrieve Unicode text in a MySQL database and display it on a web page:

<code class="php"><?php

include("connection.php"); // Database connection settings

// Set charset for MySQL queries
$result = mysql_query("SET NAMES utf8");

// Query to retrieve data from the database
$cmd = "SELECT * FROM hindi";
$result = mysql_query($cmd);

while ($myrow = mysql_fetch_row($result)) {
    echo ($myrow[0]);
}

?></code>
Copy after login

In the script, ensure you have included a database connection file named "connection.php" with the necessary settings to connect to your MySQL database.

Additional Considerations

  • Double-check that your HTML head section includes the appropriate charset:
<code class="html"><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"></code>
Copy after login
  • If you encounter any issues, try escaping Unicode characters before inserting them into the database. You can achieve this using functions like htmlentities() or htmlspecialchars().

By implementing these measures, you should be able to store and display Unicode strings in Hindi correctly using PHP and MySQL.

The above is the detailed content of How to Store and Display Unicode Strings in Hindi Using PHP and MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!