我寫的例子
複製代碼 代碼如下:<?
require("adodb/adodb.inc.php");
$conn=newadoconnection('mysql');
$conn->connect("localhost","root","2027205","bh38") or die("串連不成功");
$conn->execute("set names gb2312");
$conn->execute("INSERT INTO `vv` (`cc`) VALUES ('換了個編碼不知道行不行');") or die("錯誤");
$rc=$conn->execute("select * from vv");
while(!$rc->EOF)
{
echo($rc->fields["cc"]);
$rc->movenext();
}
?>
當然我們也可以通過如下指令修改資料庫的字元集
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)