mysql5写入和读出乱码解决
程序员文章站
2024-01-06 19:33:41
...
我写的例子
当然我们也可以通过如下指令修改数据库的字符集
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)
复制代码 代码如下:
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)