Home  >  Article  >  Database  >  MYSQL字符集错误:ERROR 1267 (HY000): Illegal mix of...解决_MySQL

MYSQL字符集错误:ERROR 1267 (HY000): Illegal mix of...解决_MySQL

WBOY
WBOYOriginal
2016-06-01 13:31:052309browse

bitsCN.com

MYSQL字符集错误:ERROR 1267 (HY000): Illegal mix of...解决

 

老生常谈,MYSQL 字符集问题:

 

1、mysql字符集

 

mysql字段、表以及数据库、数据库连接、数据库返回结果、等都有着默认字符集。

默认字符集基本上都是可以在建表建库之初进行设定的,有的也可以默认设置。但是,很多时候可能忘了设置字符集,另外连接数据库和返回结果、存储的时候,字符集往往可能不一样,所以就容易出现字符集问题。比如,存进去是中文,读出来是乱码,这写都是常有的事情。

 

如何查看数据库的字符集:

01mysql> show VARIABLES LIKE &#39;character_set_%&#39; ;02+--------------------------+--------------------------------------+03| Variable_name            | Value                                |04+--------------------------+--------------------------------------+05| character_set_client     | latin1                               |06| character_set_connection | latin1                               |07| character_set_database   | utf8                                 |08| character_set_filesystem | binary                               |09| character_set_results    | latin1                               |10| character_set_server     | latin1                               |11| character_set_system     | utf8                                 |12| character_sets_dir       | /Apps/mysql5.1/share/mysql/charsets/ |13+--------------------------+--------------------------------------+1<span></span> mysql> SHOW VARIABLES LIKE &#39;collation_%&#39;;2+----------------------+-------------------+3| Variable_name        | Value             |4+----------------------+-------------------+5| collation_connection | latin1_swedish_ci |6| collation_database   | utf8_general_ci   |7| collation_server     | latin1_swedish_ci |8+----------------------+-------------------+93 rows in set (0.00 sec)

 

 

2、问题:

 

如果建表默认是latin字符集,那么表内字段默认就是latin的,PHP 如果是utf8方式连接,mysql也会自动存成latin字符集,如果查询的话,按照utf8连接来查询latin字段中包含中文字符的话,则会返回如类似如下的错误:

 

先看看表字段的字符集:

1mysql> show create table t_search_str /G2*************************** 1. row ***************************3       Table: t_search_str4Create Table: CREATE TABLE `t_foo` (5  `str` varchar(100) CHARACTER SET latin1 NOT NULL DEFAULT &#39;&#39;,6) ENGINE=MyISAM DEFAULT CHARSET=utf871 row in set (0.00 sec)

 

很明显,数据库字符集和字段字符集不一致,这是根本原因,比如,在utf8模式下,就会返回如下错误:

 

mysql> set names utf8;Query OK, 0 rows affected (0.00 sec)mysql> SELECT *  FROM t_foo WHERE `str`=&#39;测试&#39; ; ERROR 1267 (HY000): Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation &#39;=&#39;

 

3、解决办法

 

解决办法很简单:将该字段的字符集和连接字符作左匹配做修改即可(总之,要一致就行):

 

1alter table t_foo change `str` `str` varchar(100) character set utf8 not null ;

 

再查询试试:

 

1mysql> SELECT COUNT(*) AS ct FROM t_foo WHERE `str`=&#39;测试&#39; ;2+----+3| ct |4+----+5|  0 |6+----+71 row in set (0.00 sec)

 

这样,字段和数据库的字符集就一致了。

 

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn