MySQL 4.1的字元集支援(Character Set Support)有兩個方面:字元集(Character set)和排序方式(Collation)。對於字元集的支援細化到四個層次: 伺服器(server),資料庫(database),資料表(table)和串連(connection)。
查看系統的字元集和排序方式的設定可以通過下面的兩條命令或mysql> status 。
mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
7 rows in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
上面列出的值就是系統的預設值。(很奇怪系統怎麼預設是latin1的瑞典語排序方式)...
當我們按照原來的方式通過PHP存取MySQL資料庫時,就算設定了表的預設字元集為utf8並且通過UTF-8編碼發送查詢,你會發現存入資料庫的仍然是亂碼。問題就出在這個connection串連層上。解決方案是在發送查詢前執行一下下面這句:
1. SET NAMES 'utf8';
它相當於下面的三句指令:
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
2. 建立資料庫
mysql> create database name character set utf8;
3. 建立表
CREATE TABLE `type` (
`id` int(10) unsigned NOT NULL auto_increment,
`flag_deleted` enum('Y','N') character set utf8 NOT NULL default 'N',
`flag_type` int(5) NOT NULL default '0',
`type_name` varchar(50) character set utf8 NOT NULL default '',
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
4. 修改資料庫成utf8的.
mysql> alter database name character set utf8;
5. 修改表預設用utf8.
mysql> alter table type character set utf8;
6. 修改欄位用utf8
mysql> alter table type modify type_name varchar(50) CHARACTER SET utf8;