Insert French Characters Into MySQL

June 4, 2010 | PHP | , , , ,

I was developing a bi-lingual website, so my custom CMS should be able to accept and display both English and non English characters. For English characters, it’s a piece of cake. But for French characters, I have pulling my hair for days to find out what went wrong with my script.

I have set the database to use UTF-8 encoding, but every time I inserted the text it’s messed up. French characters become a horror, unreadable for everyone.

After several days –literally– I have found the solution: I must set the character set into UTF-8 right before I insert the data!

< ?php
mysql_query("SET CHARACTER SET utf8"); //<--the key!
mysql_query("insert into data values ('$french_chars')");

SET CHARACTER SET utf8 will tell MySQL to store the inserted data using UTF-8 encoding. And it works for me.


This post has 6 comments, great!

  1. nuansa pena 04/06/2010 at 07:53

    Berarti 3 bahasa, hebat!

  2. aura pelupa 04/06/2010 at 07:55

    Pengalaman otak atik yang dapat ku ambil hasilnya! Trims telah berbagi tips!

  3. RinduDendam 06/06/2010 at 19:44

    how about default-character-set = utf8 in my.ini ?

  4. admin 07/06/2010 at 06:02

    I’m using shared hosting, it’s impossible to modify MySQL configuration (my.ini/my.cnf) file.

  5. Adam 08/03/2011 at 18:37

    Ahh, thanks for this!

  6. Shaw 02/05/2012 at 00:54

    Helped me out on this one, thanks.


Leave a comment

Allowed tags are: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>