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
(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.
Another link to to learn about unicode :
ReplyDeletehttp://www.joelonsoftware.com/articles/Unicode.html
tnks
ReplyDeleteThanx for usefull article ......but i think in following command
ReplyDelete@mysql_query("SET NAMES 'utf8'",$id);
$id is connection but not mention anywhere... it is just confussing.
Thank you very much.
DeleteDear
DeleteI had copied it from my existing code. So forget to remove my own code parts.. thanks to note it
Regards
Sahir K
thaks pankaj kumar, thnk u very much ;)
Deletevery helpful
ReplyDeletegoooood article...god bless
ReplyDeleteuse the code like this...
ReplyDeleteits works properly.... Enjoy!!!
sorry no need to use $id so removed like
ReplyDelete@mysql_query("SET NAMES 'utf8'");
and also remove '@' symbol like $cresult = mysql_query($cQry);
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.
ReplyDeletecurently 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.
Thannks and very helpfull
ReplyDeleteIt's helped me lot.
ReplyDeleteThanks.
its very helpfull
ReplyDeletethanks alot
thank you sir
ReplyDeletei think the table name is wrong
ReplyDelete$cQry= "insert into document (unicodeText) values ('".$unicodeText."')" ;
$cQry= "insert into doc_test (unicodeText) values ('".$unicodeText."')"; - right
Many many thanks.
ReplyDeleteI week English.
It's a big problem was me. in Unicode.
thanks a lot
ReplyDeleteThis comment has been removed by the author.
ReplyDeletesend code to my mail rockroy93@gmail.com
ReplyDeleteKahi ka hero h tu... uper dekhle
DeleteNot Working
ReplyDeleteThanks alot its work after few changes....
ReplyDeleteHow your text-area contain different language?
ReplyDeleteI am also facing a peculiar problem.
ReplyDeleteMy 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
nice... But I have a doubt.How to use unicode malayalam font in pdf
ReplyDelete