亂碼產生原因
mysql字元編碼是版本4.1引入的,支援多國語言,而且一些特性已經超過了其他的資料庫系統。
我們可以在MySQL Command Line Client 下輸入如下命令查看mysql的字元集
mysql> SHOW CHARACTER SET;
+----------+-----------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
+----------+-----------------------------+---------------------+--------+
36 rows in set (0.02 sec)
MySQL 4.1的字元集支援(Character Set Support)有兩個方面:字元集(Character set)和排序方式(Collation)。對於字元集的支援細化到四個層次: 伺服器(server),資料庫(database),資料表(table)和串連(connection)。
查看系統的字元集和排序方式的設定可以通過下面的兩條命令:
mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+-------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | D:\MySQL\MySQL Server 5.0\share\charsets\ |
+--------------------------+-------------------------------------------+
8 rows in set (0.06 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.02 sec)
上面列出的值就是系統的預設值。latin1預設校對規則是latin1_swedish_ci,預設是latin1的瑞典語排序方式.
為什麼呢預設會是latin1_swedish_ci呢,追溯一下mysql曆史很容易發現
1979年,一家瑞典公司Tcx欲開發一個快速的多線程、多使用者資料庫系統。Tcx 公司起初想利用mSQL和他們自己的快速低級常式 (Indexed Sequential Access Method,ISAM)去串連資料庫表,然而,在一些測試以後得出結論:mSQL對其需求來說不夠快速和靈活。這就產生了一個連接器資料庫的新SQL介面,它使用幾乎和mSQL一樣的API介面。這個API被設計成可以使那些由mSQL而寫的第三方代碼更容易地移植到MySQL。
相信如果mysql是中國開發的,那麼漢語也是預設編碼了
當然我們也可以自己需要修改mysql的預設字元集
在mysql配置文檔my.ini,找到如下兩句:
[mysql]
default-character-set=latin1
和
# created and no character set is defined
default-character-set=latin1
修改後面的值就可以。
這裡不建議改,仍保留預設值
也就是說啟動 mysql時,如果沒指定指定一個預設的的字元集,這個值繼承自設定檔中的;
此時 character_set_server 被設定為這個預設的字元集; 當建立一個新的資料庫時,
除非明確指定,這個資料庫的字元集被預設設定為 character_set_server; 當選定了一個資料庫時,
character_set_database 被設定為這個資料庫預設的字元集; 在這個資料庫裡建立一張表時,
表預設的字元集被設定為 character_set_database,也就是這個資料庫預設的字元集;
當在表內設定一欄時,除非明確指定,否則此欄預設的字元集就是表預設的字元集。
這樣問題就隨之而來了,假如一資料庫是gbk編碼。如果訪問資料庫時沒指定其的字元集是gbk。
那麼這個值將繼承系統的latin1,這樣就做成mysql中文亂碼。
亂碼解決方案
要解決亂碼問題,首先必須弄清楚自己資料庫用什麼編碼。如果沒有指明,將是預設的latin1。
我們用得最多的應該是這3種字元集 gb2312,gbk,utf8。
那麼我們如何去指定資料庫的字元集呢?下面也gbk為例
【在MySQL Command Line Client建立資料庫 】
mysql> CREATE TABLE `mysqlcode` (
-> `id` TINYINT( 255 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
-> `content` VARCHAR( 255 ) NOT NULL
-> ) TYPE = MYISAM CHARACTER SET gbk COLLATE gbk_chinese_ci;
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> desc mysqlcode;
+---------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-----------------------+------+-----+---------+----------------+
| id | tinyint(255) unsigned | NO | PRI | | auto_increment |
| content | varchar(255) | NO | | | |
+---------+-----------------------+------+-----+---------+----------------+
2 rows in set (0.02 sec)
其中後面的TYPE = MYISAM CHARACTER SET gbk COLLATE gbk_chinese_ci;
就是指定資料庫的字元集,COLLATE (校勘),讓mysql同時支援多種編碼的資料庫。
當然我們也可以通過如下指令修改資料庫的字元集
alter database da_name default character set 'charset'.
用戶端以 gbk格式發送 ,可以採用下述配置:
SET character_set_client='gbk'
SET character_set_connection='gbk'
SET character_set_results='gbk'
這個配置就等價於 SET NAMES 'gbk'。
現在對剛才建立的資料庫操作
mysql> use test;
Database changed
mysql> insert into mysqlcode values(null,'php愛好者');
ERROR 1406 (22001): Data too long for column 'content' at row 1
沒有指定字元集為gbk,插入時出錯
mysql> set names 'gbk';
Query OK, 0 rows affected (0.02 sec)
指定字元集為 gbk
mysql> insert into mysqlcode values(null,'php愛好者');
Query OK, 1 row affected (0.00 sec)
插入成功
mysql> select * from mysqlcode;
+----+-----------+
| id | content |
+----+-----------+
| 1 | php愛好著 |
+----+-----------+
1 row in set (0.00 sec)
在沒有指定字元集gbk時讀取也會出現亂碼,如下
mysql> select * from mysqlcode;
+----+---------+
| id | content |
+----+---------+
| 1 | php??? |
+----+---------+
1 row in set (0.00 sec)
【在phpmyadmin建立資料庫,並指定字元集】
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('/ddimg/uploadimg/20061129/1018560.jpg');}" alt="" src="http://www.souzz.net/ddimg/uploadimg/20061129/1018560.jpg" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
表類型根據自己需要選,這裡選MyISAM(支援全文檢索索引);
整理選擇 gbk_chinese_ci 也就是gbk字元集
gbk_bin 簡體中文, 二進位。gbk_chinese_ci 簡體中文, 不區分大小寫。
在剛才建立的資料庫插入資料庫
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('/ddimg/uploadimg/20061129/1018561.jpg');}" alt="" src="http://www.souzz.net/ddimg/uploadimg/20061129/1018561.jpg" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
再瀏覽時發現是亂碼
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('/ddimg/uploadimg/20061129/1018562.jpg');}" alt="" src="http://www.souzz.net/ddimg/uploadimg/20061129/1018562.jpg" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
為什麼呢?是因為資料庫為gbk字元集,而我們操作時沒有指定為gbk
回到資料庫首頁
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('/ddimg/uploadimg/20061129/1018563.jpg');}" alt="" src="http://www.souzz.net/ddimg/uploadimg/20061129/1018563.jpg" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
可以看到 mysql 串連校對預設的latin1_bin。我們將其改為gbk_chinese_ci
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('/ddimg/uploadimg/20061129/1018564.jpg');}" alt="" src="http://www.souzz.net/ddimg/uploadimg/20061129/1018564.jpg" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
再插入一條資料。看,這條已經正常了
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('/ddimg/uploadimg/20061129/1018565.jpg');}" alt="" src="http://www.souzz.net/ddimg/uploadimg/20061129/1018565.jpg" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
更多phpmyadmin亂碼問題請參考本論壇的
http://www.phpfans.net/bbs/viewthread.php?tid=146&extra=page%3D1
php愛好者站 http://www.phpfans.net/
【解決php讀取資料庫亂碼】
仍以資料庫mysqlcode為例
CODE:[Copy to clipboard]
<?php
$conn = mysql_connect("localhost","root","");
mysql_query("set names 'gbk'");//這就是指定資料庫字元集,一般放在串連資料庫後面就系了
mysql_select_db("test"); $sql = "select * from mysqlcode";
$result = mysql_query($sql,$conn);
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>mysql 字元編碼</title>
</head>
<body>
<table width="300" height="32" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="71" align="center">id</td>
<td width="229" align="center">內容</td>
</tr>
<?php while($row = mysql_fetch_assoc($result)){
echo "
<tr>
<td align=\"center\">".$row['id']."</td>
<td>".$row['content']."</td>
</tr>";
}?>
</table>
</body>
</html>
<?php mysql_free_result($result);?>
如果我們將mysql_query("set names 'gbk'");注釋掉,肯定時亂碼
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('/ddimg/uploadimg/20061129/1018566.jpg');}" alt="" src="http://www.souzz.net/ddimg/uploadimg/20061129/1018566.jpg" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
加上那句又正常了
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('/ddimg/uploadimg/20061129/1018567.jpg');}" alt="" src="http://www.souzz.net/ddimg/uploadimg/20061129/1018567.jpg" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
一句話
你資料庫用什麼編碼,在對資料庫操作之前就set names '你的編碼';
ps:頁面申明編碼:在HTML代碼HEAD裡面,可以用<meta http-equiv="Content-Type" content="text/html; charset="XXX" />來告訴瀏覽器網頁採用了什麼編碼,目前中文網站開發中主要用的是GB2312和UTF-8兩種編碼。