When saving or querying data, the system prompts Illegal mix of collations (gbk_chinese_ci, IMPLICIT) and (utf8_general_ci, COERCIBLE) for operation \ '= \'. here, we will tell you that the data you want to save and the character encoding in the database will not cause the data to be incorrectly saved. The solution is to unify the encoding.
The statement is simple.
The Code is as follows: |
Copy code |
<? Php $ Connection = mysql_connect ("localhost", "root", "password"); // connect to and select the Database Server Mysql_select_db ("test", $ connection ); $ Query = "select * from users where name = ''"; $ Result = mysql_query ($ query, $ connection ); While ($ row = mysql_fetch_array ($ result )) { Echo "User ID:". $ row [0]. "<br>"; // You can also write $ row ["user_id"] Echo "username:". $ row [1]. "<br>"; // You can also write $ row ["user_name"] } ?> |
Running result prompt
Error: Illegal mix of collations (gbk_chinese_ci, IMPLICIT) and (latin1_swedish_ci, COERCIBLE) for operation '='
The character sets compared with the operator "=" are inconsistent. One is gbk_chinese_ci, and the other is latin1_swedish_ci.
First, check mysql's my. cnf and confirm it as utf8
The Code is as follows: |
Copy code |
Mysql> show variables like "% char % ";
+ -------------------------- + --------------------------------------- + | Variable_name | Value | + -------------------------- + --------------------------------------- + | Character_set_client | utf8 | | Character_set_connection | utf8 | | Character_set_database | latin1 | | Character_set_filesystem | binary | | Character_set_results | utf8 | | Character_set_server | utf8 | | Character_set_system | utf8 | | Character_sets_dir | D:/MySQL Server 5.0/share/charsets/| + -------------------------- + --------------------------------------- + 8 rows in set (0.00 sec)
Mysql> show variables like "% coll % "; + ---------------------- + ------------------- + | Variable_name | Value | + ---------------------- + ------------------- + | Collation_connection | utf8_general_ci | | Collation_database | latin1_swedish_ci | | Collation_server | utf8_general_ci | + ---------------------- + ------------------- + 3 rows in set (0.00 sec) |
Then change all encoding methods in the database to uniform:
The Code is as follows: |
Copy code |
Mysql> set character_set_database = utf8;
Query OK, 0 rows affected (0.00 sec) Mysql> set collation_database = utf8_general_ci |
Check whether the character set and data table field encoding characters are consistent:
Summary
Utf8_bin
Utf8_general_ci
Utf8_unicode_ci
Utf8_bin and utf8_general_ci can
Utf8_bin and utf8_unicode_ci can
Utf8_general_ci and utf8_unicode_ci are not allowed