PHP connection MySQL Query result Chinese display garbled solution _ PHP Tutorial

Source: Internet
Author: User
PHP connection MySQL Query result Chinese display garbled solution. Let's assume that the encoding used in the database is UTF-8, then we should first add the Copy code in the PHP page as follows: metahttp-equivContent-Typecontenttexthtml; we first assume that the encoding used in the database is UTF-8.
In this case, we should first add

The code is as follows:




Here, the charset value UTF-8 must be the same as the encoding type when the file is saved.

And then add it before database query.

The code is as follows:


Mysql_query ("set names 'utf8 '");


The encoding value of the statement in this row should be the same as the preceding encoding value.

All in all, the encoding type stored on the webpage, charset = utf-8 of the webpage, and the encoding method of the executed set names utf8 statement should be consistent.

Here is a good analysis reference.

MySQL "set names x" character SET problem analysis

Recently, we have received BBT training to build a voting system. System code is not very difficult, but I spent most of my time studying character sets and encoding. The encoding (character set) problem between MySQL and Apache makes me suffer. The solution to these problems on the Internet is relatively fragmented and one-sided. most of the solutions are provided, but they do not tell why. So I will summarize the gains over the past few days to avoid future detours. This article is a little helpful for PHP writing (you will know after reading it, how to make your PHP programs display normally on servers of most space providers ), however, more help lies in the setup and setup of network servers.

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

1. # CLIENT SECTION
2. [mysql]
3. default-character-set = utf8
4. # SERVER SECTION
5. [mysqld]
6. default-character-set = utf8

These two fields are used to change the default character set of the database. The first is the default character set of the client, and the second is the default character set of 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

The utf8 is changed with the above settings. At this point, if we read data from the database through the PHP program using the UTF-8, it is likely to be a string of Or other garbled characters. After checking for half a day on the Internet, the solution is simple. after connecting to the database and reading data, run a query "set names UTF8", that is

1. mysql_query ("set names UTF8 ");

It can be displayed normally (as long as the characters in the database information are normal ). Why? In this query, what is the function of "set names UTF8?

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:

1. SET character_set_client = utf8;
2. SET character_set_results = utf8;
3. 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. So it cannot be turned back.

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 names UTF8" 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.

Summary:Add "set names UTF8" to display your webpage on more servers, even if you do not add this sentence.

Then, we should add the following code to the PHP page: 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.