Home > Article > Backend Development > What should I do if Chinese garbled characters appear when PHP stores data in the database?
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.
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"><meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″></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’,'user’,'password’);mysql_select_db(’db’);mysql_query(”set names utf8;”); //**设置字符集***
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!