One of the responses to a question I asked yesterday suggested that I should make sure my database can handle UTF-8 characters correctly. How I can do this with MySQL?
To change the character set encoding to UTF-8 for the database itself, type the following command at the mysql> prompt. USE ALTER DATABASE
.. Replace DBNAME with the database name:
ALTER DATABASE DBNAME CHARACTER SET utf8 COLLATE utf8_general_ci;
This is a duplicate of this question How to convert an entire MySQL database characterset and collation to UTF-8?
MySQL 4.1 and above has a default character set that it calls utf8
but which is actually only a subset of UTF-8 (allows only three-byte characters and smaller).
Use utf8mb4
as your charset if you want "full" UTF-8.
Set your database connection to UTF8:
if($handle = @mysql_connect(DB_HOST, DB_USER, DB_PASS)){
//set to utf8 encoding
mysql_set_charset('utf8',$handle);
}
SET NAMES UTF8
This is does the trick
I followed Javier's solution, but I added some different lines in my.cnf:
[myslqd]
skip-character-set-client-handshake
collation_server=utf8_unicode_ci
character_set_server=utf8
I found this idea here: http://dev.mysql.com/doc/refman/5.0/en/charset-server.html in the first/only user comment on the bottom of the page. He mentions that skip-character-set-client-handshake has some importance.
To make this 'permanent', in my.cnf
:
[client]
default-character-set=utf8
[mysqld]
character-set-server = utf8
To check, go to the client and show some variables:
SHOW VARIABLES LIKE 'character_set%';
Verify that they're all utf8
, except ..._filesystem
, which should be binary
and ..._dir
, that points somewhere in the MySQL installation.