In order for your Web page to display properly on more servers, add "SET NAMES UTF8".

Source: Internet
Author: User
Tags mysql command line

repinted: http://blog.csdn.net/class1/archive/2006/12/30/1469298.aspx

In order for your Web page to display properly on more servers, add "SET NAMES UTF8". (You can choose the appropriate encoding according to your preference, such as gb2312) , even if you do not add it now, you can access it normally.

First of all, the MySQL character set problem. Windows can be modified by modifying the My.ini in the

    1. # CLIENT Section
    2. [MySQL]
    3. Default-character-set=utf8
    4. # SERVER Section
    5. [Mysqld]
    6. Default-character-set=utf8

These two fields to change the default character set for the database. The first is the default character set for the client, and the second is the default character set on the server side. Suppose we set two to UTF8 and then enter "show variables like" character_set_% in MySQL Command line client to see the following characters:

Character_set_client latin1
Character_set_connection latin1
Character_set_database UTF8
Character_set_results latin1
Character_set_server UTF8
Character_set_system UTF8

The UTF8 is changed as we set above. At this point, if we use UTF-8 PHP program to read data from the database, it is likely a string of "?????" or other garbled characters. On the internet for a long while, the solution is simple, after the database is connected, before reading the data, first execute a query "SET NAMES UTF8", that is, in PHP

    1. mysql_query ("SET NAMES UTF8"); (note capitalization)

Can be displayed normally (as long as the character of the information in the database is normal). Why is that? What is the function of this query "SET NAMES UTF8"?

Enter "SET NAMES UTF8;" On the MySQL command line, then execute "show variables like" character_set_% "," and find those variables that were latin1 "character_set_client", " Character_set_connection "," character_set_results "value all changed to UTF8, the original is the 3 variables in mischief. Consult the manual, the above sentence equals:

    1. SET character_set_client = UTF8;
    2. SET character_set_results = UTF8;
    3. SET character_set_connection = UTF8;

Take a look at the effects of these 3 variables:

Information input path: client→connection→server;
Information output path: Server→connection→results.

In other words, the character set encoding is changed 3 times per path. To appear garbled output as an example, the server UTF8 data, incoming connection to latin1, incoming results into latin1,utf-8 page and results turn around. If the two character sets are incompatible, such as latin1 and UTF8, the conversion process is irreversible and destructive. So I can't turn back.

But to declare a point here, the "SET NAMES UTF8" function is only temporary, and after MySQL restarts, the default is restored.

The next step is to talk about the configuration of MySQL on the server. Do we have to add "SET NAMES UTF8" Every time we read and write to the database, to ensure the data transmission encoding is consistent? Can you configure MySQL to reach those three variables by default for the character set we want? The manual did not say that I did not find the answer on the Internet. So, from the point of view of server configuration, there is no way to omit that line of code.

Summarize the following points:

To ensure that the return of select is consistent with the program encoding, that is, the character_set_results is consistent with the program encoding.

To ensure that the program encoding and browser consistent, that is, program encoding and <meta http-equiv= "Content-type" content= "text/html; Charset=? "/> the same.

In order for your Web page to display properly on more servers, add "SET NAMES UTF8".

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.