PHP connection MySQL Query results Chinese display garbled Solution _php Tutorial

Source: Internet
Author: User
Tags mysql query
Let's first assume that the code used in the database is UTF-8
In the PHP page we should first add
Copy CodeThe code is as follows:


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

Before database queries are added
Copy the 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 page to save, the charset=utf-8 of the page, and the set names UTF8 statement to be executed should be encoded in the same way

A good analysis is quoted below

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

Recently received BBT training to make a voting system. The system code is not very difficult, but my time is mostly spent on research character sets and encodings. The coding (character set) problem of both MySQL and Apache Systems has made me so hard to eat. The solution to these problems on the Internet is relatively fragmented, more one-sided, mostly to provide solutions, but do not say why. So I will take these days to summarize the harvest, to avoid the subsequent detours. This article has a bit of help with PHP writing (you'll know how to make your PHP program appear normal in most of the space provider's servers), but more help is the setup and setting of the Web server.

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 Variebles 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");

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 variebles 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.

Summary: in order to let your Web page can be displayed on more servers, or add "SET NAMES UTF8" bar, even if you do not add this sentence can be normally accessed.

http://www.bkjia.com/PHPjc/824862.html www.bkjia.com true http://www.bkjia.com/PHPjc/824862.html techarticle We first assume that the encoding used in the database is UTF-8 then we should first add the copy code code in the PHP page as follows: Meta http-equiv= "Content-type" content= "text/html; Ch ...

  • 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.