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!
php mysql unicode internationalization utf-8
Anirudh Goel Jul 29 '09 at 8:05 2009-07-29 08:05
source share