This article summarizes the common Chinese garbled problems in PHP+MySQL storage data. Share it for your reference, as follows:
Common reasons for Chinese garbled code in PHP+MySQL:
1. The encoding of the MYSQL database is utf8. If the encoding format is inconsistent with the PHP web page, it will cause Chinese garbled code in MYSQL.
2. When creating tables in MYSQL or selecting fields, the type set is not utf8, and the web page encoding is not utf8, which may also cause garbled Chinese in MYSQL.
3. The character set of the PHP page is inconsistent with the encoding of the database.
4. PHP connects to MYSQL database. The operation is the encoding and page encoding specified by the set statement. The PHP page encoding is inconsistent.
5. If the HTML page encoding submitted by the user is inconsistent with the page encoding that displays the data, it will definitely cause garbled PHP pages.
How to solve the problem of garbled Chinese:
1. Web encoding settings. Generally, attributes are added to the file header <html> in HTML code:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Guaranteed 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 file encoding method required to be saved is utf-8 (you can use EditPlus to open the settings, as shown in the figure below), which ensures that the file is also utf-8 encoding.
3. The Chinese part of the table in the database stores the fields, and should be set to the utf8_general_ci type.
When connecting to a database operation, the field type of the operation is set to utf8. The setting method is as follows:
mysql_connect('localhost','user','password'); mysql_select_db('db'); mysql_query("set names utf8"); //**Set character set***mysql_query(To be executedsqlStatement);
I hope this article will be helpful to everyone's PHP programming.