MySQL encoding mechanism and garbled characters

Source: Internet
Author: User

The following articles mainly describe the MySQL encoding mechanism. We all know that MySQL supports many types of encoding, including latin1, gbk, utf8, and big5, of course there are some things we have never heard of. The following is a description of the specific content.

When using PHP to connect to MySQL, The queried data may sometimes be garbled. To solve the garbled problem, we must first understand the MySQL encoding mechanism.

Take Discuz! For example, Discuz! After the Forum connects to MySQL, it will immediately execute a setting as shown in include/db_mysql.class.php ):

Copy the content to the clipboard Code:

@ Mysql_query ("SET character_set_connection = $ dbcharset, character_set_results = $ dbcharset, character_set_client = binary", $ this-> link ); to a large extent, the character settings at these three points solve the garbled problem. What are the specific functions of these three settings? If we understand the MySQL encoding mechanism, we can avoid garbled Characters During plug-in installation and normal operations. We can also transcode the Forum data on our own.

Character_set_client can be understood as the encoding of SQL statements. If it is set to binary, MySQL will process it as binary. the value set by character_set_connection tells MySQL the encoding used to run SQL statements, that is, the SQL statement sent by the program to MySQL is first converted to the encoding specified by character_set_client by MySQL from character_set_connection. If character_set_clien specifies binary, mySQL interprets the SQL statement according to the encoding specified by character_set_connection.

During SQL statement execution, for example, when data is inserted into the database, encoding settings are also provided for the field. If the encoding settings of the field are different from those specified by character_set_connection, mySQL converts the inserted data into the encoding set by the field.

When the SQL statement is executed to return data to the client, the data is converted from the encoding specified by the field to the encoding specified by character_set_results. If character_set_results = NULL, no conversion is performed, note that setting it to NULL is not equal to not being set. MySQL inherits the global settings if it is not set ).

After learning about the MySQL encoding mechanism, we can use this mechanism to transcode data and avoid garbled characters during program usage.

Supplement:

Mysql_query ("character_set_client = gbk"); this statement tells MySQL that the encoding of the SQL statement to be used is GBK.

Mysql_query ("character_set_connection = gbk"). This tells MySQL to convert the SQL statement into GBK for processing.

Mysql_query ("character_set_results = gbk"). This statement indicates that all results returned by MYSQL queries must be encoded in GBK format.
 

Related Article

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.