Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The solution of displaying garbled codes in Chinese of MySQL query results by PHP connection

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the "PHP connection MySQL query results Chinese display garbled solution" related knowledge, in the actual case of the operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

Let's first assume that the encoding used in the database is UTF-8

At this point, we should first add to the PHP page

The copy code is as follows:

The value UTF-8 of charset here must be the same as the encoding type when the file was saved.

Then add before the database query

The copy code is as follows:

Mysql_query ("set names' utf8'")

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

All in all, the type of encoding saved by the web page, the charset=utf-8 of the web page, and the encoding of the executed set names utf8 statements should be the same.

Here's a good analysis.

Analysis of MySQL's "SET NAMES x" character set

Recently, I was trained by BBT to make a voting system. The system code is not very difficult, but my time is mainly spent on studying character sets and coding. The coding (character set) problems of MySQL and Apache systems made me rack my brains and suffer a lot. Online solutions to these problems are more fragmented and one-sided, mostly to provide solutions, but do not say why. So I summed up the gains of the past few days to avoid further detours for the latecomers. This article is a little helpful for PHP writing (you'll know how to make your PHP program work on most space providers' servers), but more help lies in setting up and setting up web servers.

Let's start with the character set of MySQL. Under Windows, you can modify the

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-side default character set, and the second is the server-side default character set. Suppose we set both to utf8, and then type "show variebles like" character_set_% ";" in MySQL Command Line Client, and 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

The utf8 changes according to the settings above. At this point, if we read data from the database by using UTF-8 's PHP program, it is likely to be a string of "?" Or some other random code. After searching on the Internet for a long time, the solution is simple. After connecting to the database and before reading the data, execute a query "SET NAMES UTF8", that is, in PHP for

1.mysql_query ("SET NAMES UTF8")

It can be displayed normally (as long as the characters of the information in the database are normal). Why is this? What exactly is the purpose of this query "SET NAMES UTF8"?

Type "SET NAMES UTF8;" to the MySQL command line, and then execute "show variebles like" character_set_% ";", and find that the values of the variables "character_set_client", "character_set_connection" and "character_set_results" that were originally latin1 have all changed to utf8, and it turns out that these three variables are making trouble. 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 role of these three 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 three times. Take the output with garbled codes as an example. The data of utf8 in server is transferred into connection to latin1, and then to results to latin1,utf-8 page and then to results. If the two character sets are not compatible, such as latin1 and utf8, the conversion process is irreversible and destructive. So I can't turn back.

But let's make it clear here that the role of "SET NAMES UTF8" is only temporary, and the default value will be restored after MySQL restart.

Let's talk about the configuration of MySQL on the server. Don't we have to add "SET NAMES UTF8" every time we read and write to the database to ensure that the encoding of the data transmission is consistent? Can we configure MySQL to achieve which three variables default to the character set we want? It's not in the manual, and I didn't find the answer on the Internet. Therefore, from the perspective of server configuration, 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 don't add this sentence now.

"PHP connection MySQL query results Chinese display garbled solution" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report