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());
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) 版权所有 1985-2001 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> INSERT 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> INSERT 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 | | NULL | |
| vc_id | int(11) | NO | | NULL | |
| votes_sum | int(11) | NO | | 0 | |
+------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql>

代码一:


Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 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 458 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> show tables;
Empty set (0.00 sec)

mysql> set names utf8; //设置数据库字符集为utf8
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE vote
-> (
-> vote_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> vote_title varchar(100) not null,
-> vc_id int not null,
-> votes_sum int not null default 0
-> )ENGINE=MyISAM DEFAULT CHARSET=utf8//创建数据表,字符集设定为utf8
-> ;
Query OK, 0 rows affected (0.03 sec)

mysql> INSERT INTO vote VALUES(null,'你是从哪里了解到本站的?',1,default);
ERROR 1406 (22001): Data too long for column 'vote_title' at row 1//TNND,一样的字符插入中文也有错?疑问中.....
mysql> INSERT INTO vote VALUES(null,'bu shi ba da ge',1,default);//插入英文无错....
Query OK, 1 row affected (0.00 sec)

mysql>

代码二:

mysql> drop table vote;
Query OK, 0 rows affected (0.00 sec)

mysql> set names gb2312;//设置数据库字符集为gb2312
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE vote
-> (
-> vote_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> vote_title varchar(100) not null,
-> vc_id int not null,
-> votes_sum int not null default 0
-> )ENGINE=MyISAM DEFAULT CHARSET=gb2312
-> ;
Query OK, 0 rows affected (0.03 sec)

mysql> INSERT INTO vote VALUES(null,'你是从哪里了解到本站的?',1,default);
Query OK, 1 row affected (0.00 sec)//gb2312不亏是专门为中文制定的标准啊..我喜欢不过不国际化

mysql> select * from vote;
+---------+-------------------------+-------+-----------+
| vote_id | vote_title | vc_id | votes_sum |
+---------+-------------------------+-------+-----------+
| 1 | 你是从哪里了解到本站的? | 1 | 0 |
+---------+-------------------------+-------+-----------+
1 row in set (0.00 sec)

mysql> set names utf8;//设定字符集编码为utf8
Query OK, 0 rows affected (0.00 sec)

mysql> select * from vote;//效果出来了^_^!
+---------+------------------------------------+-------+-----------+
| vote_id | vote_title | vc_id | votes_sum |
+---------+------------------------------------+-------+-----------+
| 1 | 浣犳槸浠庡摢閲屼簡瑙e埌鏈 珯鐨? | 1 | 0 |
+---------+------------------------------------+-------+-----------+
1 row in set (0.00 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值