: Solve the problem of php access to mysql4.1 garbled code. Read and solve the problem of php access to mysql4.1 garbled code. the multi-language support introduced from MySQL4.1 is indeed great, and some features have exceeded other database systems. The multi-language support introduced since MySQL 4.1 is indeed great, and some features have exceeded other database systems. However, during the test, I found that using a PHP statement before MySQL 4.1 to operate the MySQL database may cause garbled characters, even if the table character set is set.
MySQL 4.1 Character Set Support has two aspects: Character set and Collation ). The support for character sets is refined to four levels: server, database, table, and connection ).
You can run the following two commands to view the character set and sorting method settings of the system:
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 default values of the system. (It's strange how latin1's Swedish sorting method is used by default .)
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 the UTF-8 encoding, you will find that the database is still garbled. The problem lies in the connection layer. The solution is to execute the following statement before sending the query:
Set names 'utf8 ';
It is equivalent to the following three commands:
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
Try again.
The above content solves the problem of php access to mysql 4.1 garbled characters. For more information, see PHP Chinese network (www.php1.cn )!