The multi-language support introduced since MySQL 4.1 is really great, and some features have surpassed other database systems. However, during the test, I found that using PHP statements suitable for 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 values listed above are the system default values. (It’s strange how 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 the query is sent through UTF-8 encoding, 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;
Try again and it will be normal.
The above is the content to solve the garbled problem of PHP access to MySQL 4.1. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!