PHP read MySQL garbled, with set names XXX to solve the principle of sharing _php skills

Source: Internet
Author: User
Tags mysql command line
First of all, MySQL's character set problem. Windows can be modified by modifying the My.ini within the

PHP code
Copy Code code as follows:

[MySQL]

Default-character-set=utf8//Client's default character set
[Mysqld]

Default-character-set=utf8//server-side default character set

Suppose we set all two to UTF8 and then enter "show Variebles like" character_set_% in the MySQL Command line client and see the following characters:
Copy Code code as follows:

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 a database using a UTF-8 PHP program, it's probably a bunch of "?????." Or else it's garbled.

The workaround is to execute a query "SET NAMES UTF8" before reading the data after the database is connected, that is, in PHP

mysql_query ("SET NAMES UTF8");

This sentence must be placed on the database server connection statement "$connection =mysql_connect ($db _host, $db _user, $db _psw) or Die (" Connection Server failed ");

You can display normal (as long as the character of the information in the database is normal).

To the MySQL command line, enter "SET NAMES UTF8;" And then execute "show variebles like" character_set_% ", and find those variables" Latin1 "that were originally character_set_client," Character_set_connection "," Character_set_results "the value of all into UTF8, the original is the 3 variables in mischief.

Consult the manual, the above sentence is equal to:

SET character_set_client = UTF8;

SET character_set_results = UTF8;

SET character_set_connection = UTF8;

Look at the effects of these 3 variables:
Information input path: client→connection→server;
Information output path: Server→connection→results.
In other words, each path has to change the character set encoding 3 times. In the case of garbled output, the server UTF8 data, the incoming connection into the latin1, into the results into the Latin1,utf-8 page and results turn over. If two sets of characters are incompatible, such as latin1 and UTF8, the conversion process is irreversible and destructive.

But here to declare that the "SET NAMES UTF8" function is only temporary, MySQL restart after the default.

Next, we'll talk about MySQL's configuration on the server. Do we have to add "SET NAMESUTF8" to the database every time we read and write to ensure that the encoding of the data is consistent? Can you configure MySQL to reach those three variables by default for the character set we want to think about? Not on the manual, I didn't find the answer on the Internet either. So, from the server configuration point of view, there is no way to omit that line of code.
Summary: In order for your Web page to be displayed properly on more servers, add "SET NAMES UTF8", even if you do not add this sentence to the normal access.

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.