转 :http://blog.163.com/clicked@126/blog/static/36584595201011161174865/
show create table table_name; -- 查看表的编码类型
ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
show Variables;
Variable_name | Value |
---------------------------------+--------------------------------------+
auto_increment_increment | 1 |
auto_increment_offset | 1 |
automatic_sp_privileges | ON |
back_log | 50 |
basedir | d:\wamp\mysql\ |
binlog_cache_size | 32768 |
bulk_insert_buffer_size | 8388608 |
character_set_client | latin1 |
character_set_connection | latin1 |
character_set_database | utf8 |
character_set_filesystem | binary |
character_set_results | latin1 |
character_set_server | gbk |
character_set_system | utf8 |
character_sets_dir | d:\wamp\mysql\share\charsets\ |
collation_connection | latin1_swedish_ci |
collation_database | utf8_unicode_ci |
collation_server | gbk_chinese_ci |
修改变量 set character_set_client='utf8'
转:http://blog.csdn.net/biangren/article/details/8010075
MySQL 4.1的字符集支持(Character Set Support)有两个方面:字符集(Character set)和排序方式(Collation)。对于字符集的支持细化到四个层次: 服务器(server),数据库(database),数据表(table)和连接(connection)。
查看系统的字符集和排序方式的设定可以通过下面的两条命令或mysql> status 。
mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
7 rows in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
上面列出的值就是系统的默认值。(很奇怪系统怎么默认是latin1的瑞典语排序方式)...
当我们按照原来的方式通过PHP存取MySQL数据库时,就算设置了表的默认字符集为utf8并且通过UTF-8编码发送查询,你会发现存入数据库的仍然是乱码。问题就出在这个connection连接层上。解决方法是在发送查询前执行一下下面这句:
1. SET NAMES 'utf8';
它相当于下面的三句指令:
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
2. 创建数据库
mysql> create database name character set utf8;
3. 创建表
CREATE TABLE `type` (
`id` int(10) unsigned NOT NULL auto_increment,
`flag_deleted` enum('Y','N') character set utf8 NOT NULL default 'N',
`flag_type` int(5) NOT NULL default '0',
`type_name` varchar(50) character set utf8 NOT NULL default '',
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
4. 修改数据库成utf8的.
mysql> alter database name character set utf8;
5. 修改表默认用utf8.
mysql> alter table type character set utf8;
6. 修改字段用utf8
mysql> alter table type modify type_name varchar(50) CHARACTER SET utf8;