Mysql数据转移编码问题

MySql的字符集是越来越严格了,涉及到好几个层面的编码。

实用的来说,我们一般就用gb2312或者utf8两种编码(个人严重推荐utf8) ,下面以utf8为例说一下:

1、转移数据的时候,在Create table后面注明字符集为 utf8,数据库校验(collation)字符集为utf8_unicode_ci,比如:

CREATE TABLE `member` (
`id` int(7) NOT NULL auto_increment,
`account` varchar(50) default NULL,
`password` varchar(100) default NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM CHARACTER SET utf8 COLLATE utf8_unicode_ci;

2、客户端读写数据的时候,在select database前面加上SET NAMES ‘utf8′这个语句,这是为了告诉MySql连接校验(connection collation)的字符集也是utf8,比如:

$mysql->host=”localhost”;
$mysql->user=”foor”;
$mysql->pass=”bar”;
$mysql->db=”db”;
$mysql->connect();
mysql_query(”SET NAMES ‘utf8′”);
$mysql->select_db();

这样就搞定了,gb2312也是类似的修改这两层就可以了

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>