How to solve the problem of Chinese garbled characters in PHP database

藏色散人
Release: 2023-03-07 06:44:01
Original
3781 people have browsed it

Solutions to Chinese garbled characters in the PHP database: 1. Add the charset attribute to the file header in the HTML code; 2. Add the code "charset=utf-8" to the beginning of the PHP code; 3. In the database The part of the table field that stores Chinese characters is set to the "utf8_general_ci" type.

How to solve the problem of Chinese garbled characters in PHP database

Recommendation: "mysql video tutorial"

The reason why Chinese garbled characters appear in PHP MySQL.

1. The encoding of the MYSQL database is utf8, which is inconsistent with the encoding format of the PHP web page, which will cause Chinese garbled characters in MYSQL.

2. When using MYSQL to create a table or select a field, the type set is not utf8, and the web page encoding is not utf8, which may also cause MYSQL Chinese garbled characters.

3. Character set of the PHP page The encoding is inconsistent with the database.

4. PHP connects to the MYSQL database. The operation is to set the encoding specified by the statement and the page encoding. The PHP page encoding is inconsistent.

5. If the encoding of the HTML page submitted by the user is inconsistent with the encoding of the page displaying the data, it will definitely cause the PHP page to be garbled.

How to solve the problem of Chinese garbled characters.

1. Web page encoding settings. Generally, add the attribute:

<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
Copy after login

to the file header in the HTML code to ensure that the web page is "utf-8" encoded.

2. PHP code settings. Add the following code at the beginning of the php code:

header(”Content-type: text/html;charset=utf-8″);
Copy after login

and the encoding method of the saved file is required to be utf-8 (you can use EditPlus to open the settings, as shown below), thus ensuring that the file is also utf-8 encoded .

3. The Chinese part stored in the fields of the table in the database must be set to the utf8_general_ci type.

4. When PHP connects to the database for operation, it is necessary to set the field type of the operation to utf8. The setting method is as follows:

mysql_connect(’localhost’,&#39;user’,&#39;password’);mysql_select_db(’db’);mysql_query(”set names utf8;”); //**设置字符集***
Copy after login

How to solve the problem of Chinese garbled characters in PHP database

Example comparison. Through the above analysis, we followed the solution and obtained the correct Chinese encoding stored in MySQL. The effect is as shown in the figure below:

How to solve the problem of Chinese garbled characters in PHP database

The above is the detailed content of How to solve the problem of Chinese garbled characters in PHP database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template