Wednesday, July 15, 2009

Store and retrieve Unicode characters with MySQL using PHP

 

Newcomers to web programming world may face some very common issues related to Unicode and local language support.
Here I am pointing some generic queries that you might be asked to your colleagues in many days.
1. How to store Unicode characters in a database
2. How php can store/retrieve Unicode characters to and from Mysql database
3. How Indian languages like Malayalam or Hindi or Tamil can be stored in database.

There are some significant difference between Unicode and Ansi fonts (visit : http://users.csc.calpoly.edu/~bfriesen/software/builds.html for difference between them). Since local language computing become much popular and even laymen starts using internet. So storing local languages(Malayalam, Hindi, etc ) will be a necessary feature for the web sites. This can be a big problem to web programmers as most might not be aware of the techniques required for storing the Unicode languages in a database.

Here I am using MySql as DB and PHP as programming language.

Design of the database tables :

when creating a table, you should ensure your table can receive Unicode characters. below are the steps to create a table, which supports Unicode.

CREATE TABLE IF NOT EXISTS `doc_test` (
`id` int(10) unsigned NOT NULL auto_increment,
`unicodeText` varchar(450) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=30 ;

Here you have to make sure below things :
1. Your database encoding/collation should be utf8_general_ci.
2. Make sure your designed table(here doc_test ) should be having collation as utf8_general_ci.
3. Or even you can check the fields collation settings and make sure its utf8_general_ci.
4.Below image will help you to understand about what I meant.
I have taken a screen shot of table structure from phpmyadmin
 unicode_db1
(Click on the image to view in bigger size)

Here note the the circled part.
5. If your database is not having the proper collation settings, visit below link to convert it into utf8_general_ci.
http://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html.
Or install phpmyadmin software, which will help you to convert the collation settings by some mouse clicks.
6. Hope you have converted your collation settings.

Now We need PHP code to insert or retrieve the Unicode character.

1. Script to insert Unicode text to table. Here $txt is a variable which will be filled from a submit action. so user should have a text area in HTML page with a name of 'ta'.

(Eg : <textarea name="ta" rows="10" cols="80”>)

<?php
$txt= $_POST['ta']; // get the unicode text from a submit action.
$unicodeText = $txt;
@mysql_query("SET NAMES 'utf8'",$id); /// Its very inportant to display or insert unicode characters in db
$cQry= "insert into document (unicodeText) values ('".$unicodeText."')" ;
$cresult = @mysql_query($cQry,$id);

?>
2. PHP code to Display the Unicode text from MySQL database:

PHP CODE :
<?php
@mysql_query("SET NAMES 'utf8'",$id); /// Its very inportant to display or insert unicode characters in db
$cresult = @mysql_query("SELECT unicodeText FROM document",$id);
while ($crow = @mysql_fetch_array($cresult,MYSQL_ASSOC))
{
echo ( $crow['unicodeText']);
}
?>
Note: SET NAMES 'utf8' is an important command to work with Unicode texts. Don't forget to include that in your code.
3. Finished !!!
Hope you have enjoyed by seeing your local language visible in your web page.

26 comments:

  1. Another link to to learn about unicode :
    http://www.joelonsoftware.com/articles/Unicode.html

    ReplyDelete
  2. Thanx for usefull article ......but i think in following command
    @mysql_query("SET NAMES 'utf8'",$id);
    $id is connection but not mention anywhere... it is just confussing.

    ReplyDelete
    Replies
    1. Thank you very much.

      Delete
    2. Dear
      I had copied it from my existing code. So forget to remove my own code parts.. thanks to note it
      Regards
      Sahir K

      Delete
    3. thaks pankaj kumar, thnk u very much ;)

      Delete
  3. goooood article...god bless

    ReplyDelete
  4. use the code like this...



    its works properly.... Enjoy!!!

    ReplyDelete
  5. sorry no need to use $id so removed like
    @mysql_query("SET NAMES 'utf8'");

    and also remove '@' symbol like $cresult = mysql_query($cQry);

    ReplyDelete
  6. Can you pls. help me how to do when I wish store it in a text file and while EDITING again it shows unicode in a cleditor but renders in html correctly for users.

    curently cleditor mapped to mytextarea. I copy indian language in this mytextarea. When I retrieve as html for users it shows the stored indian language content correctly. But when I go for editing mytextarea cleditor, the cleditor bears only unicode characters.

    as a workaround I have to paste again in cleditor the all the corrected content instead of just few word that demanded correction.

    kindly help as how to show correctly use editing (as a content admin) not as a user.

    ReplyDelete
  7. Thannks and very helpfull

    ReplyDelete
  8. It's helped me lot.
    Thanks.

    ReplyDelete
  9. i think the table name is wrong

    $cQry= "insert into document (unicodeText) values ('".$unicodeText."')" ;

    $cQry= "insert into doc_test (unicodeText) values ('".$unicodeText."')"; - right

    ReplyDelete
  10. Many many thanks.
    I week English.
    It's a big problem was me. in Unicode.

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. send code to my mail rockroy93@gmail.com

    ReplyDelete
  13. Thanks alot its work after few changes....

    ReplyDelete
  14. How your text-area contain different language?

    ReplyDelete
  15. I am also facing a peculiar problem.
    My data is stored as unicode in the table using utf8_general_ci collation.

    I can see hindi in the table using phpmyadmin.

    My problem is when i retrieve the records using a query from php page the result is always 0 records in case of unicode fields. The query works fine in case i try to retrieve english records.
    The sample query is

    SELECT * FROM orders WHERE App_Name like "%काशी%" AND 1= 1 ORDER BY Yr

    if i copy and paste this query in phpmyadmin it works like a charm.

    Where i can be wrong

    ReplyDelete
  16. nice... But I have a doubt.How to use unicode malayalam font in pdf

    ReplyDelete