search
Homephp教程php手册mysql5写入和读出乱码解决

mysql5写入和读出乱码解决

Jun 13, 2016 pm 12:35 PM
requireGarbled characterscodeexamplewriteandcopyIsolve

我写的例子

复制代码 代码如下:

 
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)
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft