Differences
This shows you the differences between two versions of the page.
mysql_character_sets [2013/10/07 14:27] |
mysql_character_sets [2013/10/07 14:27] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== MySQL Character Sets ====== | ||
+ | |||
+ | * [[MySQL]] | ||
+ | |||
+ | MySQL, by default, uses LATIN1 encoding. You can verify this by opening up a ''mysql'' shell and | ||
+ | running this statement: | ||
+ | |||
+ | <code> | ||
+ | SHOW VARIABLES WHERE variable_name = 'character_set_client'; | ||
+ | </code> | ||
+ | |||
+ | === MySQL and PHP === | ||
+ | |||
+ | See what encoding PHP is using to connect as: | ||
+ | |||
+ | <code> | ||
+ | $link = mysql_connect('localhost', 'user', 'pass'); | ||
+ | $charset = mysql_client_encoding($link); | ||
+ | </code> | ||
+ | |||
+ | Likewise, you can override the setting and change it. The code must be directly after creating the MySQL connection: | ||
+ | |||
+ | <code> | ||
+ | $link = mysql_connect('localhost', 'user', 'pass'); | ||
+ | $bool = mysql_set_charset('utf8', $link); | ||
+ | $charset = mysql_client_encoding($link); | ||
+ | </code> | ||
+ | |||
+ | Using ''mysqli'' driver instead of ''mysql'': | ||
+ | |||
+ | <code> | ||
+ | $link = mysqli_connect('localhost', 'user', 'pass', 'world'); | ||
+ | mysqli_set_charset('utf8', $link); | ||
+ | $charset = mysqli_character_set_name($link); | ||
+ | </code> | ||
+ | |||
+ | === Global Variables === | ||
+ | |||
+ | You can change the database to use UTF8 by changing the runtime variables: | ||
+ | |||
+ | <code> | ||
+ | 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'; | ||
+ | </code> | ||
+ | |||
+ | === Database Configuration === | ||
+ | |||
+ | MySQL needs to be configured to use UTF8, or it will still use LATIN1 by default: | ||
+ | |||
+ | <code> | ||
+ | [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 | ||
+ | </code> | ||