MYSQL乱码问题
mysql> use demodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql> show variables like 'character\_set\_%';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
+--------------------------+--------+
7 rows in set (0.00 sec)
mysql> set character_set_client=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> show variables like 'character\_set\_%';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| character_set_client | utf8 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
+--------------------------+--------+
7 rows in set (0.00 sec)
mysql> select * from project;
+-------+------------+------+--------------+-------------+-----------+----------+--------------+--------+
| ID | pname | URL | LEAD | DESCRIPTION | pkey | pcounter | ASSIGNEETYPE | AVATAR |
+-------+------------+------+--------------+-------------+-----------+----------+--------------+--------+
| 10200 | demo | NULL | aaa | NULL | DEMO | 159 | 2 | 10011 |
| 10300 | demo2 | NULL | bbb | NULL | DEMOTWO | 1 | 2 | 10011 |
| 10302 | ?????? | | ccc | | FFF | 0 | 2 | 10011 |
| 10500 | ???? | NULL | aaa | NULL | QQQ | 11 | 2 | 10011 |
| 10501 | SCRUM-???? | NULL | ddd | NULL | SFFF | 24 | 2 | 10011 |
| 10502 | ?????? | NULL | eee | NULL | SSS | 6 | 2 | 10011 |
| 10504 | ???? | NULL | fff | NULL | FINANCING | 2 | 2 | 10011 |
+-------+------------+------+--------------+-------------+-----------+----------+--------------+--------+
7 rows in set (0.00 sec)
mysql>
mysql> set character_set_connection=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_database=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_results=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_server=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'character\_set\_%';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
+--------------------------+--------+
7 rows in set (0.00 sec)
mysql> select * from project;
+-------+--------------------+------+--------------+-------------+-----------+----------+--------------+--------+
| ID | pname | URL | LEAD | DESCRIPTION | pkey | pcounter | ASSIGNEETYPE | AVATAR |
+-------+--------------------+------+--------------+-------------+-----------+----------+--------------+--------+
| 10200 | demo | NULL | aaa | NULL | DEMO | 159 | 2 | 10011 |
| 10300 | demo2 | NULL | bbb | NULL | DEMOTWO | 1 | 2 | 10011 |
| 10302 | 实例项目需求 | | ccc | | FFF | 0 | 2 | 10011 |
| 10500 | 演示项目 | NULL | ddd | NULL | QQQ | 11 | 2 | 10011 |
| 10501 | SCRUM-实例项目 | NULL | eee | NULL | SFFF | 24 | 2 | 10011 |
| 10502 | 正式A需求 | NULL | fff | NULL | SSS | 6 | 2 | 10011 |
| 10504 | 正式T | NULL | ddd | NULL | FINANCING | 2 | 2 | 10011 |
+-------+--------------------+------+--------------+-------------+-----------+----------+--------------+--------+
7 rows in set (0.00 sec)
通过以上过程说明.仅仅通过set character_set_client=utf8;是不能解决乱码问题的.解决乱码问题需要把mysql的服务器编码设置成utf8,同时,创建数据库的时候需要把数据库的编码设置成utf8编码,才能解决mysql数据库查询乱码问题.
如果数据库服务器的编码是utf8,而数据库的编码也是utf8,在客户端查询出现乱码,基本上可以推断是客户端的设置不是utf8编码.
数据库服务编码设置请参见mysql服务端编码设置.