Home > php教程 > php手册 > Summary of common Chinese garbled problems in PHP MySQL data storage,

Summary of common Chinese garbled problems in PHP MySQL data storage,

WBOY
Release: 2016-07-06 14:24:39
Original
909 people have browsed it

A summary of common Chinese garbled code problems in PHP MySQL stored data,

This article summarizes common Chinese garbled code problems in PHP MySQL stored data. Share it with everyone for your reference, the details are as follows:

Common reasons for Chinese garbled characters 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 using MYSQL to create a table or select a field, the type set is not utf8, and the web page encoding is not utf8, which may also cause MYSQL Chinese garbled characters.

3. The character set of the PHP page is inconsistent with the encoding of 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 displaying 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 attributes to the file header in the HTML code:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 

Copy after login

Guaranteed, the web page is encoded in "utf-8".

2. PHP code settings. Add the following code at the beginning of the php code:

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

Copy after login

The encoding method of the file required to be saved is UTF-8 (you can use EditPlus to open the settings, as shown below), which ensures that the file is also UTF-8 encoded.

3. The part that stores Chinese characters 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 needs 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"); //**设置字符集***
mysql_query(要执行的sql语句);

Copy after login

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

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template