PHP connection MySQL Query results Chinese display garbled solution _php tips

Source: Internet
Author: User
Tags mysql query mysql command line

We first assume that the encoding used in the database is UTF-8
In this case we should first add the PHP page

Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>

The value Utf-8 here charset must be the same as the encoding type when the file is saved

Then add before the database query

Copy Code code as follows:

mysql_query ("Set names ' UTF8 '");

The encoded value of the line statement should also be the same as the encoded value above.

All in all, the encoding type of the Web page is saved, the charset=utf-8 of the Web page, and the set names UTF8 statement that is executed should be encoded in the same way

Here's a good analysis

Analysis of MySQL's "set NAMES X" Character Set problem

Recently received BBT training to do a voting system. The system code is not very difficult, but my time is mainly spent on the research character set and coding. The coding (character set) problem of MySQL and Apache two systems let me get my head in the rough. Online solutions to these problems are more fragmented, more one-sided, most of which provide solutions, but do not say why. So I will make a summary of these days, to avoid the subsequent detours. This article is a bit of a Help for PHP (you'll know how to make your PHP program appear normal on most of your space provider's servers), but more help is set up and installed on the Web server.

First of all, MySQL's character set problem. Windows can be modified by modifying the My.ini within 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 client default character set, and the second is the 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:

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 changes as we set above. At this point, if we use UTF-8 PHP program to read data from the database, it is likely to be a string of "?????" or other garbled. Online search for a half-day, the solution is simple, after connecting the database, read the data before the first execution of a query "SET NAMES UTF8", that is, in PHP for

1.mysql_query ("SET NAMES UTF8");

You can display normal (as long as the character of the information in the database is normal). Why is that? This query "SET NAMES UTF8" in the end what is the role?

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:

1.SET character_set_client = UTF8;
2.SET character_set_results = UTF8;
3.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. So I can't turn it back.

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 NAMES UTF8" Every time we read and write to the database 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.

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.