Troubleshoot PHP access MySQL 4.1 garbled problem

Source: Internet
Author: User
SolvePHPAccess mySQL4.1garbledProblem
Multi-language support introduced from MySQL 4.1 is really great, and some of the features already outweigh the otherDatabase Tutorialssystem。 But I found out during the test that PHP used before MySQL 4.1StatementOperating MySQLDataLibrary will cause garbled, even if the table has been setcharacterThe same is true of the set. After reading the tenth chapter "Character Set Support" in the new MySQL online manual, I finally found the solution and tested it.
MySQL 4.1 's character set support (Character set Support) has two aspects: Character set (Character set) and Sort method (collation). The support for character sets is refined to four levels:Server(server), database, datasheet (table), and connection (connection).
The settings for viewing the system's character set and sorting can be set by using 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 default values for the system. (It's strange how the system defaults to the Swedish sort of latin1) ...
When we access the MySQL database in the original way through PHP, even if the default character set for the table is UTF8 and sent via UTF-8 encodingQuery, you will find that stored in the database is still garbled. The problem is on the connection connection layer. The workaround is to execute the following sentence before sending the query:
SET NAMES ' UTF8 ';
It corresponds to the following three-sentence instruction:
SET character_set_client = UTF8;
SET character_set_results = UTF8;
SET character_set_connection = UTF8;
Try again, is it normal? ^_^ enjoy!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.