Friday, January 23, 2009

Displaying "non-English" characters using PHP & MySQL

Many times we intend to display "non-English" characters say Hindi language on the web-page using PHP & MySQL. Here's the trick:

1. Set column, table & database collation to "utf8_unicode_ci". You can use PHPMyAdmin for that purpose.

2. Use following meta tag in the php page:
meta http-equiv="Content-Type" content="text/html; charset=UTF-8"


3. And while establishing connection with the database make sure you do the following:
$chs1="SET NAMES utf8";
myquery($chs1);

$chs2="SET CHARACTER SET utf8";
myquery($chs2);

function myquery($var) {
$rval=mysql_query($var);
return $rval;
}

Resource:
http://dev.mysql.com/doc/refman/5.0/en/charset.html
http://www.phpfreaks.com/forums/index.php/topic,235410.msg1095028.html#msg1095028

Important commands:
mysql> show character set;
mysql> show collation ;
mysql> show global variables;

1 comment:

  1. hello Mr. parag
    i want to store hindi words in mysql. i have installed unicode in my pc and also tried the things mentioned above. Could u plz help me?

    ReplyDelete