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

Storing and displaying unicode string (हिन्दी) using PHP and MySQL

Did you set proper charset in the HTML Head section?

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

or you can set content type in your php script using -

   header( 'Content-Type: text/html; charset=utf-8' ); 

There are already some discussions here on StackOverflow - please have a look

How to make MySQL handle UTF-8 properly
setting utf8 with mysql through php

PHP/MySQL with encoding problems

So what i want to know is how can i
directly store सूर्योदय into my
database and fetch it and display in
my webpage using PHP.

I am not sure what you mean by "directly storing in the database" .. did you mean entering data using PhpMyAdmin or any other similar tool? If yes, I have tried using PhpMyAdmin to input unicode data, so it has worked fine for me - You could try inputting data using phpmyadmin and retrieve it using a php script to confirm. If you need to submit data via a Php script just set the NAMES and CHARACTER SET when you create mysql connection, before execute insert queries, and when you select data. Have a look at the above posts to find the syntax. Hope it helps.

** UPDATE **
Just fixed some typos etc

How to display Unicode data with PHP

Try to set charachter encoding after mysql_connect function like this:

 mysql_query ("set character_set_client='utf8'"); 
mysql_query ("set character_set_results='utf8'");

mysql_query ("set collation_connection='utf8_general_ci'");

How to store the data in unicode in hindi language

Choose utf8 character set and utf8_general_ci collation.

Obviously, Collation of the field (to which you want to store Hindi text) should be utf8_general_ci.

To alter your table field, run

ALTER TABLE `<table_name>` CHANGE `<field_name>` `<field_name>` VARCHAR(100) 
CHARSET utf8 COLLATE utf8_general_ci DEFAULT '' NOT NULL;

Once you've connected to database, run the following statement at first

mysql_set_charset('utf8');

Eg:

//setting character set
mysql_set_charset('utf8');

//insert Hindi text
mysql_query("INSERT INTO ....");

To retrieve data

//setting character set
mysql_set_charset('utf8');

//select Hindi text
mysql_query("SELECT * FROM ....");

Before you printing any unicode text (say Hindi text) on browser, you should have to set content type of that page by adding a meta tag

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Eg:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example Unicode</title>
</head>

<body>
<?php echo $hindiText; ?>
</body>
</html>

Update:

mysql_query("SET CHARACTER SET utf8") has changed tomysql_set_charset('utf8');
This is the preferred way to change the charset. Using mysql_query() to set it (such as SET NAMES utf8) is not recommended. See http://php.net/manual/en/function.mysql-set-charset.php*

MySQL - insert japanese from PHP - Encoding Troubles

try this before the insert query

mysql_query("SET NAMES utf8");

Also not sure if you set the proper charset of the database, and of the web page.

Charset in HTML Head section?

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

and/or something like

   header( 'Content-Type: text/html; charset=utf-8' );

Followings will help you get more ideas how to do it .. if something doesnt work commment back.

check more here on SO

Storing and displaying unicode string (हिन्दी) using PHP and MySQL

How to make MySQL handle UTF-8 properly

setting utf8 with mysql through php

PHP/MySQL with encoding problems

How to view hindi font on Browser in json format

set the charset after creating the connection

$conn =new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
$conn->set_charset("utf8");

more details on http://php.net/manual/ro/mysqli.set-charset.php

otherwise it's likely still latin depending on your php instalation



Related Topics



Leave a reply



Submit