文檔:
sample enviroment:
it's not simle to yours but I think it will work.
windows server 2003 Simple Chinese edition
apache 2.2.11
php-5.2.8-Win32
MySQL-5.1.31
=============
0.編碼選擇:UTF-8 還是 GB2312還是GBK。
UTF-8可以相容ASCII編碼,會根據需要佔用1個或者2個位元組,比較小。(其他如UTF-16和UTF-32是固定死佔用2個位元組或者4個位元組的)
GB2312隻能包括6000多常用字
GBK可以包含簡體、繁體,範圍廣
1. PHP中的設定
php中要對php.ini檔案中設定開啟extension=php_mbstring.dll,按照需要修改mb_string的相關編碼配置
2.MySql中的設定:
因為mySql字元集的支援細化到四個層次: 伺服器(server),資料庫(database),資料表(table)和串連(connection)。因此可以後面再設定
3.Apache中的設定:
如果是UTF-8就不用設定,如果是GBK需要設定一下httpd.conf ,開啟AddDefaultCharset
4.具體操作:
第一步:建立資料庫
預設是latine的encoding和collection的。因為只有一個欄位是中文的,所以不用修改資料庫的預設設定檔。
只要建立資料庫的時候,對這個欄位設定為gbk_chinese_ci即可
第二步:設定網頁的charset和encoding。如果是沒有設定,將根據用戶端的Request來
第三步,建立串連的時候需要
SET character_set_client='gbk'
SET character_set_connection='gbk'
SET character_set_results='gbk'
(這個配置就等價於 SET NAMES 'gbk'。
)
================================================
0.choose an encoding
you have 3 choises:utf-8,gb2312 and GBK.
UTF-8 contains ASKII, and it uses 1 or 2 bytes so it's smaller to transform in web.
but it's contains less Chinese Characters.(Others like UTF-16 and UTF-32,is fixed to user 2 or 4 bytes)
GB2312 contains only 6000 more popular characters, such as '朱鎔基' can't be contained.
GBK contains simple Chinese and Tranditional Chinese ,I think it's pro. my sample is for GBK.
1. config PHP
modify php.ini , and set the line
extension=php_mbstring.dll
on
.maybe you need to configurate the mb_string.
2.mySql configration
as mySql supports defferent charset in 4 layers, the server, the database,the table and connection.
and you have only one table ,one field in Chinese, you need not to modify the whole mysql configration
3.Apache configuration
If you are willing to use UTF-8 , you need not to configurate anything for it support utf-8 by default.
if you want to use GBK you need to modify httpd.conf and open the AddDefaultCharset
4.sample steps:
firstly, create a database
and the database's collection is latine.
when you create the table, the field, set the field's collection to gbk_chinese_ci
secondly, set the special pages you want to show the Chinese Characters.
set the charset and encoding
third, when you build a connection ,either in commandline or php , you need to run the 3 lines script:
SET character_set_client='gbk'
SET character_set_connection='gbk'
SET character_set_results='gbk'
these three lines are equle to
SET NAMES 'gbk'
例子:
php檔案
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>測試中文入資料庫</title>
</head>
<body>
<ul>
<?php
$conn = mysql_connect("localhost","root","sa");
mysql_query("set names 'gbk'");//關鍵是這句
mysql_select_db("test");
$sql = "select * from chinesetest ";
$result = mysql_query($sql,$conn);
while($row = mysql_fetch_assoc($result))
{
echo "<li>".$row['id']."-".$row["CF"]."</li>";
}
$sql2 = "Insert Into chinesetest (CF) values('中文插入測試');";
mysql_query($sql2);
$sql2 = "Insert Into chinesetest (CF) values('請看,這裡是繁體中文')";
//echo $sql2;
mysql_query($sql2);
echo mysql_error();
mysql_close();
?>
</ul>
</body>
</html>
整體解決方案:
/Files/xxpyeippx/chinese_on_mysql.zip
建表語句:
CREATE TABLE IF NOT EXISTS `chinesetest` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`CF` varchar(255) CHARACTER SET gbk NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;