How MySQL uses set names to solve Garbled text

Source: Internet
Author: User

How MySQL uses set names to solve Garbled text

We often use "set names utf8" to solve garbled code. Why can we solve this problem by adding this code? Next, let's dive into the internal execution principle of set names utf8.

Let's talk about the character set of MySQL. In Windows, you can modify

[Mysql]

Default-character-set = utf8 // default character set of the Client
[Mysqld]

Default-character-set = utf8 // default character set on the server

Suppose we set both to utf8, and then input "show variebles like" character_set _ % ";" in the MySQL Command Line Client. The following characters are displayed:

Character_set_client latin1
Character_set_connection latin1
Character_set_database utf8
Character_set_results latin1
Character_set_server utf8
Character_set_system utf8

If we read data from the database through a PHP program using a UTF-8, It is likely a string of "???" Or other garbled characters.

The solution is to execute a query "set names UTF8" Before connecting to the database and reading data, that is

Mysql_query ("set names UTF8 ");

// This sentence must be placed after the database server connection statement [$ connection = mysql_connect ($ db_host, $ db_user, $ db_psw) or die ("connection server failed ");]

It can be displayed normally (as long as the characters in the database information are normal ).

Enter "set names UTF8;" in the MySQL command line, and then execute "show variebles like" character_set _ % ";", the original latin1 variables "character_set_client", "character_set_connection", and "character_set_results" are all changed to utf8.

Refer to the Manual. The above sentence is equal:

SET character_set_client = utf8;

SET character_set_results = utf8;

SET character_set_connection = utf8;

Let's take a look at the functions of the three variables:
Information Input path: client → connection → server;
Information output path: server → connection → results.
In other words, each path must undergo three character set encoding changes. Taking garbled output as an example, for utf8 data in the server, the incoming connection is converted to latin1, the incoming results is converted to latin1, And the UTF-8 page is converted to results. If the two character sets are incompatible, such as latin1 and utf8, the conversion process is irreversible and destructive.

However, we need to declare that "set names UTF8" is only temporary, and the default value will be restored after MySQL is restarted.

Next we will talk about the configuration of MySQL on the server. Isn't it because we have to add "SET NAMESUTF8" to each database read/write to ensure Data Transmission Encoding consistency? Can we configure MySQL to achieve the three variables which are the character set we want by default? I did not mention the manual, and I did not find the answer online. Therefore, the line of code cannot be omitted from the server configuration perspective.
Conclusion: In order to make your webpage display normally on more servers, add "set names UTF8", even if you do not add this sentence.

This article permanently updates the link address:

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.