PHP connection MySQL query results show Chinese scrambled code solution

  • 2020-10-31 21:40:27
  • OfStack

We first assume that the encoding used in the database is ES1en-8
At this point we should add it first in the PHP page


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

The value of charset here, utf-8, must be like the encoding type 1 at the time the file was saved

It is then added before the database query


mysql_query("set names 'utf8'");

The coded value of this statement should also be the same as the coded value 1 above.

In summary, the type of encoding saved for the page, charset= ES17en-8 for the page, and the encoding of the set names utf8 statement executed should be 1

A good analysis is cited below

Analysis of "SET NAMES x" Character set of MySQL

Recently I was trained by BBT to do a voting system. The system code was not difficult, but I spent most of my time studying character sets and encodings. The coding (character set) problems of MySQL and Apache systems make me struggle and suffer a lot. Online solutions to these problems are fragmented and one-sided, mostly offering solutions without saying why. So I will these days of harvest summary 1, to avoid the next comer to take a detour. This article is a bit helpful for PHP writing (you'll see how to get your PHP application to display properly on most space provider servers), but it's more helpful in setting up and setting up the web server.

Start with the MySQL character set issue. Windows can be modified in ES40en. ini

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

These two fields change the default character set of the database. The first is the client default character set, and the second is the server default character set. Suppose we set both to utf8 and type "show variebles like" character_set_% "in MySQL Command Line Client. , you can 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

utf8 has changed with our Settings above. At this point, if we read data from the database through the PHP program using ES83en-8, it is likely to be a series of "??????? ". Or some other gibberish. After a long search on the Internet, the solution is simple, after connecting to the database, before reading the data, first perform a query "SET NAMES UTF8", that is, in PHP is

1.mysql_query("SET NAMES UTF8");

Displays normally (as long as the characters of the database information are normal). Why is that? What is the function of the query "SET NAMES UTF8"?

Go to the MySQL command line and type "SET NAMES UTF8; , then execute "show variebles like" character_set_% "; ", found that the original latin1 variables "character_set_client", "character_set_connection", "character_set_results" all changed the value of utf8, which turned out to be the three variables. Refer to 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 three variables in action:

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

In other words, each path goes through three character set encoding changes. Take the output with messy code as an example. The data in server is converted from connection to latin1, the data in results to latin1, and the page of utf-8 turns results to results. If the two character sets are incompatible, such as latin1 and utf8, the conversion process is irreversible and destructive. So there's no turning back.

But just to be clear, "SET NAMES UTF8" is only temporary. MySQL will resume by default when it is restarted.

This brings us to the configuration of MySQL on the server. Don't we have to add "SET NAMES UTF8" every time we read or write to the database to ensure that the data is transmitted with a code of 1? Is it possible to configure MySQL so that the three variables default to the character set we want? It's not in the manual, and I can't find the answer online. So, from a server configuration point of view, there is no way to get rid of that line of code.

Bottom line: In order for your web pages to display properly on more servers, add "SET NAMES UTF8", even if you don't add it right now.


Related articles: