" in the HTML code; 2. Add the header code at the beginning of the PHP code; 3. Set the fields of the table in the database to "utf8_general_ci" type."/> " in the HTML code; 2. Add the header code at the beginning of the PHP code; 3. Set the fields of the table in the database to "utf8_general_ci" type.">

Home  >  Article  >  Backend Development  >  How to solve the problem of garbled characters stored in the database in PHP

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

藏色散人
藏色散人Original
2020-08-22 10:00:083821browse

Solution to the garbled code stored in the database by php: 1. Add the meta attribute to the file header "" in the HTML code; 2. Add the header code to the beginning of the php code; 3. Change the table in the database The field is set to type "utf8_general_ci".

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

Recommendation: "PHP Video Tutorial"

Solution to the problem of garbled characters when PHP MySQL Chinese data is saved to the database Method

Common reasons 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 creating a table or selecting a field in MYSQL, 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 that displays the data, it will definitely cause the PHP page to be garbled.

Methods 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">

to the file header 100db36a723c770d327fc0aef2ce13b1 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");

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(&#39;localhost&#39;,&#39;user&#39;,&#39;password&#39;);
mysql_select_db(&#39;db&#39;);
mysql_query("set names utf8"); //**设置字符集***
mysql_query(要执行的sql语句);

I hope this article will be helpful to everyone in PHP programming.

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

Statement:
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