PDA

View Full Version : Storing UTF-8 in MySQL


Edomondo
02-16-2005, 09:44 AM
I'm facing a problem about storing UTF-8 encoded character in MySQL.
I've googled around and many persons are having the same problem.

One of my page is UTF-8 encoded. It contains text fields whose content are stored to MySQL.
When I enter Japanese character in the box, they are not stored the correct way (either characters like メーãƒâ€*ルレジェンド 交響詩 宿命 or question marks ????????????????).
The problem is that PhpDig does store unicode character in MySQL very well! I've looked at its code but found no clues.
I've tried with utf8_encode & utf8_decode, htmlentities... but nothing worked.

Please help :-(

renehaentjens
02-17-2005, 01:16 AM
I did a test with a HTML-page containing a TEXTAREA. The page was generated by PHP, with: header('Content-Type: text/html; charset=UTF-8');

I could input letters with accents and Greek letters. They came back correctly after having been stored in MySQL, retrieved back, and displayed again in a new UTF-8 HTML-page.

My test was with the Firefox 1.0 browser and an EasyPHP 1.7 (Windows) server, which is PHP 4.3.3 and MySQL 4.0.15. The tables in MySQL were created with all defaults in an 8859-1 environment.

Of course, if you look in the MySQL tables, e.g. with phpMyAdmin, the data looks bizarre because with this DB setup MySQL doesn't "know" that it is storing UTF-8, it just stores octet-strings. But for getting user input and re-displaying it, that doesn't matter.

Edomondo
02-17-2005, 01:35 AM
Thank you for your reply.

I've tried to display a UTF-8 content stored to MySQL by PhpDig and the result was OK, so my problem must come from the storing process.

I've set the default charset of my tables to UTF-8 (just like PhpDig does), but still can't figure why it works for PhpDig and not for me...

Any clue?