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

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 properlysetting 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


<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><?php $con = mysql_connect("localhost","root","");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_query('SET character_set_results=utf8');mysql_query('SET names=utf8');mysql_query('SET character_set_client=utf8');mysql_query('SET character_set_connection=utf8');mysql_query('SET character_set_results=utf8');mysql_query('SET collation_connection=utf8_general_ci');mysql_select_db('onlinetest',$con);$nith = "CREATE TABLE IF NOT EXISTS `TAMIL` (  `data` varchar(1000) character set utf8 collate utf8_bin default NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1";if (!mysql_query($nith,$con)){  die('Error: ' . mysql_error());}$nithi = "INSERT INTO `TAMIL` VALUES ('இந்தியா நாட்டின் பக்கங்கள்')";if (!mysql_query($nithi,$con)){  die('Error: ' . mysql_error());}$result = mysql_query("SET NAMES utf8");//the main trick$cmd = "select * from TAMIL";$result = mysql_query($cmd);while($myrow = mysql_fetch_row($result)){    echo ($myrow[0]);}?></body></html>


For those who are looking for PHP ( >5.3.5 ) PDO statement, we can set charset as per below:

$dbh = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');