Home  >  Article  >  Backend Development  >  Solve the problem of Chinese garbled characters in php mysql

Solve the problem of Chinese garbled characters in php mysql

藏色散人
藏色散人Original
2020-11-05 09:59:192085browse

php Mysql Chinese garbled solution: 1. Modify the encoding format in the database or table; 2. Add "mysqli_query("set names utf8");" to the PHP statement; 3. In the client Just add "header('charset=utf-8');".

Solve the problem of Chinese garbled characters in php mysql

Recommendation: "PHP Video Tutorial"

The problem of Chinese garbled characters in PHP and Mysql

When building a database with MySQL, sometimes there will be some garbled code problems. Here are some solutions.

Problem 1: The encoding format in the database or table is incorrect

When creating the database, use the following code to create it:

CREATE DATABASE `test`
CHARACTER SET 'utf8'
COLLATE 'utf8_general_ci';

When creating the table:

CREATE TABLE `table_name` (
id varchar(40) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`UserID` varchar(40) NOT NULL default '',
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Once these three settings are set, Mysql will basically have no problems, that is, the same encoding format will be used when building databases and tables.

If the database has been created, you can use the following methods:

1. Check the default encoding format:

mysql> show variables like "%char%";  
+--------------------------+---------------+  
| Variable_name | Value |  
+--------------------------+---------------+  
| character_set_client | gbk |  
 character_set_connection | gbk |  
| character_set_database | utf8 |  
 character_set_filesystem | binary |  
| character_set_results | gbk |  
| character_set_server | utf8 |  
| character_set_system | utf8 |  
+--------------------------+-------------+

Note: To determine the previous 2, you can use set names utf8 , set names gbk sets the default encoding format; you can set the database encoding more simply through workbench.

The effect of executing SET NAMES utf8 is equivalent to setting the following at the same time:

SET character_set_client='utf8';  
SET character_set_connection='utf8';  
SET character_set_results='utf8';

2. Check the encoding format of the test database:

mysql> show create database test;  
+------------+------------------------------------------------------------------------------------------------+  
| Database | Create Database |  
+------------+------------------------------------------------------------------------------------------------+  
| test | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET gbk */ |  
+------------+------------------------------------------------------------------------------------------------+

3. Check the encoding of the yjdb data table Format:

mysql> show create table yjdb;  
| yjdb | CREATE TABLE `yjdb` (  
`sn` int(5) NOT NULL AUTO_INCREMENT,  
`type` varchar(10) NOT NULL,  
brc` varchar(6) NOT NULL,  
`teller` int(6) NOT NULL,  
`telname` varchar(10) NOT NULL,  
`date` int(10) NOT NULL,  
`count` int(6) NOT NULL,  
`back` int(10) NOT NULL,  
PRIMARY KEY (`sn`),  
UNIQUE KEY `sn` (`sn`),  
NIQUE KEY `sn_2` (`sn`)  
) ENGINE=MyISAM AUTO_INCREMENT=1826 DEFAULT CHARSET=gbk ROW_FORMAT=DYNAMIC |

Problem 2: PHP and Mysql transfer encoding are incompatible

Need to add

mysqli_query(“set names utf8”);

mysql set names to the PHP statement to solve the cause of garbled characters:

http://blog.csdn.net/zsmj_2011/article/details/7943734

Question 3: Client decoding problem:

Add header('charset=utf-8'); that's it

The above is the detailed content of Solve the problem of Chinese garbled characters in php mysql. 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