The multi-language support introduced since MySQL 4.1 is really great, and some features have surpassed other database systems. However, during the test, it was found that using PHP statements applicable to MySQL before 4.1 to operate the MySQL database will cause garbled characters, even if the table character set is set.
MySQL 4.1’s character set support (Character Set Support) has two aspects: character set (Character set) and sorting method (Collation). Support for character sets is refined to four levels: server, database, table and connection.
To view the system's character set and sorting settings, you can use the following two commands:
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)
The above column The value shown is the system default value. (It’s strange why the system defaults to the Swedish sorting method of latin1)...
When we access the MySQL database through PHP in the original way, even if the default character set of the table is set to utf8 and encoded through UTF-8 Send a query and you will find that the data stored in the database is still garbled. The problem lies in this connection layer. The solution is to execute the following sentence before sending the query:
SET NAMES 'utf8';
It is equivalent to the following three instructions:
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
END