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

I need to store the hindi text in a MySQL database, extract it using a PHP script and display it on a web page. I have done the following:

I created a database and set its encoding to UTF-8, as well as mapping to utf8_bin . I added the varchar field to the table and set it to accept UTF-8 text in the charset property.

Then I decided to add data to it. Here I had to copy data from an existing site . Hindi text looks like this: सूर्योदय: 05: 30

I directly copied this text into my database and used the PHP echo(utf8_encode($string)) code echo(utf8_encode($string)) to display the data. After that, the browser showed me "??????".

When I inserted the UTF equivalent of the text by going to the "view source" in the browser, however, सूर्योदय translates to सूर्योदय .

If I go in and save सूर्योदय in the database, it converts perfectly.

So, I want to know how I can directly store सूर्योदय in my database and retrieve it and display it on my web page using PHP.

Also, can someone help me figure out if there is a script that when typed in सूर्योदय gives me सूर्योदय ?

solution found

I wrote the following sample script that worked for me. Hope this helps someone else too.

 <html> <head> <title>Hindi</title></head> <body> <?php include("connection.php"); //simple connection setting $result = mysql_query("SET NAMES utf8"); //the main trick $cmd = "select * from hindi"; $result = mysql_query($cmd); while ($myrow = mysql_fetch_row($result)) { echo ($myrow[0]); } ?> </body> </html> 

Dump for my database storing hindi utf strings,

 CREATE TABLE `hindi` ( `data` varchar(1000) character set utf8 collate utf8_bin default NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO `hindi` VALUES ('सूर्योदय'); 

Now my question is: how did it work without specifying "META" or header information?

Thank!

+46
php mysql unicode internationalization utf-8
Jul 29 '09 at 8:05
source share
5 answers

Have you set the correct encoding in the HTML Head section?

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

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

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

There are already discussions in StackOverflow, please take a look

How to get MySQL to handle UTF-8 utf8 installation correctly using mysql via php

PHP / MySQL with coding problems

So I want to know how I can directly store सूर्योदय in my database and get it and display it in my web page using PHP.

I'm not sure what you mean by "direct storage in the database." Did you mean data entry using PhpMyAdmin or any other similar tool? If so, I tried to use PhpMyAdmin to enter data in Unicode, so it did a great job with me - you can try to enter data using phpmyadmin and get it using php script to confirm. If you need to send data using a PHP script, just set NAMES and CHARACTER SET when you create a mysql connection, before executing insert queries and when selecting data. Take a look at the posts above to find the syntax. Hope this helps.

** UPDATE ** Some typos just fixed, etc.

+37
Jul 29 '09 at 8:10
source share
 <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> 
+22
Aug 25 '09 at 4:26
source share

For those who are looking for PHP (> 5.3.5) PDO instructions, we can set the encoding as shown below:

 $dbh = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password'); 
+7
Jul 02 '15 at 12:59 on
source share
 CREATE DATABASE hindi_test CHARACTER SET utf8 COLLATE utf8_unicode_ci; USE hindi_test; CREATE TABLE `hindi` (`data` varchar(200) COLLATE utf8_unicode_ci NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; INSERT INTO `hindi` (`data`) VALUES('कंप्यूटर'); 
+1
Oct. 20 '16 at 10:57
source share

For those who are having difficulty, just go to php admin and change the collation to utf8_general_ci. Select Table, go to Operations >> Table Parameters >> There Should Be Mappings

+1
Sep 02 '19 at 6:34
source share



All Articles