Home  >  Article  >  Backend Development  >  What should I do if Chinese garbled characters appear when PHP stores data in the database?

What should I do if Chinese garbled characters appear when PHP stores data in the database?

coldplay.xixi
coldplay.xixiOriginal
2020-08-10 10:17:232752browse

The solution to Chinese garbled characters when PHP stores data in the database: first add attributes to the file header []; then add [utf-8] encoding files to the PHP code; then add the Chinese part of the table in the database Set to [utf8_general_ci] type; finally set the operation field to utf8.

What should I do if Chinese garbled characters appear when PHP stores data in the database?

Solution to the problem of Chinese garbled characters when PHP stores data in the database:

1. Web page encoding settings. Generally, add the attribute: <pre class="brush:php;toolbar:false">&lt;meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″&gt;</pre> 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 to the beginning of the php code:

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

and the encoding method required to save the file is utf-8 (you can use EditPlus to open the settings, as shown below), thus ensuring that the 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;”); //**设置字符集***

What should I do if Chinese garbled characters appear when PHP stores data in the database?

Related learning recommendations : php programming(video)

The above is the detailed content of What should I do if Chinese garbled characters appear when PHP stores data in the database?. 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