Home > Backend Development > PHP Problem > 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
Release: 2023-03-04 07:52:02
Original
2853 people have browsed it

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: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″&gt;</pre><div class="contentsignin">Copy after login</div></div> 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″);
Copy after login

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;”); //**设置字符集***
Copy after login

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!

Related labels:
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