到家消除mysql下utf-8的乱码难点

建表时先加上default charset=utf8;

插入中文数据在此以前(Mysql命令提醒符下)是用set names gb2312;

在php页面中装置是mysql_query(‘SET NAMES UTF8’);

在php页面(已设定为utf8后)中插入普通话数据不相会世乱码,应该插入数据库的时候即便以utf8字符集插入粤语数据的故不会出现难题!

在php页面后边不要忘了拉长header(‘content-type;text/html;charset=utf-8’);

我们在MySQL命令提醒符下想查看数据,中文数据有恐怕是之类 ‘?????’

于是大家想查看到科学的汉语要与插入中文数据一致采取set names gb2312;

例子:

CREATE DATABASE mydb;

USE mydb5;

CREATE TABLE news
(
      news_id int not null auto_increment,
      news_title varchar(100) not null,
      news_contents text not null,
      news_date datetime not null,
      primary key(news_id)
)default charset=utf8;

set names gb2312;  

INSERT INTO news VALUES(null, ‘新闻1’, ‘111111’, now());
MySQL,INSERT INTO news VALUES(null, ‘新闻2’, ‘222222’, now());
INSERT INTO news VALUES(null, ‘新闻3’, ‘333333’, now());
INSERT INTO news VALUES(null, ‘新闻4’, ‘444444’, now());
INSERT INTO news VALUES(null, ‘新闻5’, ‘555555’, now());

Microsoft Windows XP [版本 5.1.2600]
(C) 版权全部 1983-二零零一 Microsoft Corp.

C:\Documents and Settings\YSDH.YSDH-0>cd \

C:\>mysql -h localhost -uroot -p
Enter password: ******
Welcome to the MySQL monitor.                          Commands end with
; or \g.
Your MySQL connection id is 6 to server version: 5.0.22-community-nt-log

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql> use mydb;
Database changed
mysql> INSE安德拉T INTO vote
VALUES(null,’你是从哪里精晓到本站的?’,1,default);
Query OK, 1 row affected (0.00 sec)

mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> INSEPAJEROT INTO vote
VALUES(null,’你是从哪个地方驾驭到本站的?’,1,default);
ERROR 1406 (22001): Data too long for column ‘vote_title’ at row 1
(varchar(100) 还不够?)
mysql> describe vote;
+————+————–+——+—–+———+—————-+
| Field                              |
Type                                 | Null | Key | Default |
Extra                                  |
+————+————–+——+—–+———+—————-+
| vote_id                            |
int(11)                              | NO                           |
PRI | NULL                            | auto_increment |
| vote_title | varchar(100) |
NO                           |                    

网站地图xml地图