Monday, August 19, 2013

[Codeigniter][Solved] Mysql::Error Incorrect string value - Can't insert chinese character

It's glad that I got the solution for my problem finally ~

Step 1)  
you should comfirm your database is using utf8
you can use the sql command shown below to check it:

SHOW VARIABLES LIKE '%char%';

image of my case :


Step 2)  
Then go to root of directory of your codeigniter  project.
your_project_name/application/config/datadbase.php   (<-- change the red text to your project name)

find out the database.php file and edit the sentence so that
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = "utf8_unicode_ci";



Step 2)  
you should comfirm your database is using utf8
ALTER TABLE  `e_employerinfo` CHANGE  `e2012`  `e2012` VARCHAR( 20 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL
This command can change to column 's collation to "uft8_unicode_ci" :


I use column with collation=  latin1_swedish_c to store the chinese character ,
but using latin1_swedish can't store any data in chinese..
it's better if using utf8, big5, gkb .,
in this case, i use utf8.

============ Conclusion =============
if you get the error,
1) check is your database character set is utf8
2) confirm you have set char_set in config/database.php to utf8
3) then make sure your table and column are use utf8 too

btw, is make sure everything inyour project is using utf8, including the header of HTML page
don't forget to use this sentence within your head tag of HTML page:

<meta http-equiv="content-type" content="text/html; charset=utf-8" />


No comments :

Post a Comment