Tutorial: How to store UTF8 (indian language) data in mysql?
Well, let me put it straight this tutorial also applies to any other native languages like latin, spanish etc. But, since I have tried for Indian languages I am writing exclusive for it.
Okay, so before we say code I must tell you this tutorial intends, a thought provoking stuff 🙂
Lately, we have seen enough number of Indic-translators, and you must have got your hands on with typing in ur mail/blog. Now, just think for a while, when u sent a mail, and try to read it back from “Sent Mail”, u still see the indian language or even in that case blog articles. For that case, even in Orkut.
How are they stored inside database? Was what I was trying.
Again, I am not sure for any other DB, I have tried with MySQL. But, I guess should be same.
We assume we have a DB with table articles, and a column named posts, which will save the article written in your blog. Best part, we know that all major DB’s support UTF8. And we shall explore that feature.
Now we write a article in hindi, हेल्लो वर्ल्ड
If the UTF8 is not specified, you should see something like ?????? in ur DB else u shud see the hindi data. Clear? Koool lets see the code
First check for UTF8 compatibility with this query. If it supports you should see the output as “Character_set_system”| “UTF8”
SHOW VARIABLES LIKE ‘character_set_system’;
Now that being checked, alter the table and just modify the column, Posts in our above example and specify it as UTF8
ALTER TABLE articles MODIFY Posts VARCHAR(20) CHARACTER SET UTF8;
Now, try to insert the hindi value and save it. Query it and u shud see the hindi text 🙂
We have go ahead and use many other options as well, like Collate etc. But this tutorial intends to keep it simple.
I hope you find this useful and interesting. If you have tried any hacks similar to this, pls drop in comments and share with us.
If you are reading article, you might also be interested in the PHP and Scriptaculous Book I have authored.
The book gives you insights about effects, drag-n-drop, slideshows, applications, auto-completion, in-place editing and more. Complete code snippets and explanations.
CHECK out and BUY the PHP and Script.aculo.us book at Packt official site