If Unicodes end up as funny characters after inserting into MySQL, then try executing SET NAMES ‘utf8′ immediately after obtaining a DB connection. For example:
$link = mysql_connect(‘localhost’,'myuser’,'mypassword’);
mysql_query(“SET NAMES ‘utf8′”);
mysql_select_db(‘mydb’);
References: http://www.adviesenzo.nl/examples/php_mysql_charset_fix/
September 3, 2008 at 11:41 am |
Hi Sing Chyun, I am not sure about the mysql client side on PHP, but it is actually a good idea to make your mysql database use unicode by default:
CREATE DATABASE my_database DEFAULT CHARACTER SET utf8;
and create tables with the following:
ENGINE=InnoDB DEFAULT CHARSET=utf8;
If you already have existing database, either back up, drop the database and create with the above steps, or alter the table with:
ALTER TABLE `my_table` CHARACTER SET utf8 COLLATE utf8_general_ci;
For some text columns, you might need to alter them as follows too:
ALTER TABLE `my_table` MODIFY COLUMN `my_column` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL default ”;
Hope that helps.
September 4, 2008 at 3:48 pm |
Hi Choong Yong, thank you for the comprehensive steps!