MySQL Character Sets
MySQL, by default, uses LATIN1 encoding. You can verify this by opening up a mysql
shell and
running this statement:
SHOW VARIABLES WHERE variable_name = 'character_set_client';
MySQL and PHP
See what encoding PHP is using to connect as:
$link = mysql_connect('localhost', 'user', 'pass'); $charset = mysql_client_encoding($link);
Likewise, you can override the setting and change it. The code must be directly after creating the MySQL connection:
$link = mysql_connect('localhost', 'user', 'pass'); $bool = mysql_set_charset('utf8', $link); $charset = mysql_client_encoding($link);
Using mysqli
driver instead of mysql
:
$link = mysqli_connect('localhost', 'user', 'pass', 'world'); mysqli_set_charset('utf8', $link); $charset = mysqli_character_set_name($link);
Global Variables
You can change the database to use UTF8 by changing the runtime variables:
SET @@character_set_client = 'utf8'; SET @@character_set_connection = 'utf8'; SET @@character_set_database = 'utf8'; SET @@character_set_results = 'utf8'; SET @@character_set_server = 'utf8';
Database Configuration
MySQL needs to be configured to use UTF8, or it will still use LATIN1 by default:
[mysqld_safe] character-set-server=utf8 [mysqld] character-set-server=utf8 [client] default-character-set=utf8 [mysqldump] default-character-set=utf8 [mysql] default-character-set=utf8 [mysqladmin] default-character-set=utf8 [mysqlcheck] default-character-set=utf8 [mysqlimport] default-character-set=utf8 [mysqlshow] default-character-set=utf8