Home > Backend Development > PHP Tutorial > Solve the problem of inserting Chinese garbled characters in php mysql query_PHP tutorial

Solve the problem of inserting Chinese garbled characters in php mysql query_PHP tutorial

WBOY
Release: 2016-07-13 10:11:44
Original
902 people have browsed it

Solving the problem of inserting Chinese garbled characters in php mysql query

1. The Chinese problem is actually that it has been uniformly encoded, otherwise it will be garbled

 1. Unify the encoding of database and php page

2. The database data table fields and page coding are unified

If you can do the above two, there will be no Chinese garbled problem, then let’s look at the example of solving the Chinese garbled problem in mysql

1. My mysql table is as follows

 --

 --Table structure `useradmin`

 --

The code is as follows

CREATE TABLE IF NOT EXISTS `userain` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`username` varchar(20) DEFAULT NULL,
`userpass` varchar(40) DEFAULT NULL,
`logins` int(4) NOT NULL DEFAULT '0' COMMENT 'Number of logins',
`logintime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`mid` char(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;

代码如下  

CREATE TABLE IF NOT EXISTS `userain` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`username` varchar(20) DEFAULT NULL,
`userpass` varchar(40) DEFAULT NULL,
`logins` int(4) NOT NULL DEFAULT '0' COMMENT '登陆次数',
`logintime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`mid` char(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;

All the above are encoded in utf8, so I will test a Chinese

The code is as follows

INSERT INTO `userain` (`id`, `I am Chinese`, `userpass`, `logins`, `logintime`, `mid`) VALUES
(1, 'admin', '7c1f03139281878059b909c42ccf2f6a', 0, '2010-04-14 14:20:26', '1');

代码如下  

INSERT INTO `userain` (`id`, `我是中文`, `userpass`, `logins`, `logintime`, `mid`) VALUES
(1, 'admin', '7c1f03139281878059b909c42ccf2f6a', 0, '2010-04-14 14:20:26', '1');

I said that it can be entered normally, but some people said that it is definitely not possible. In fact, the key is not the sql statement, but the encoding settings when php connects to mysql data, as follows.

The page is edited as gbk

The code is as follows

//Configure mysql database connection parameters
$db = mysql_connect("localhost", "user","password");
mysql_select_db("message",$db);
//Add the following line before executing the sql statement

代码如下

//配置mysql数据库连接参数
$db = mysql_connect("localhost", "user","password");
mysql_select_db("message",$db);
//在执行sql语句之前加上下面这一行

?>

?>

After connecting to the database, the submission must be garbled or cannot be saved. If you want to solve the problem of Chinese garbled mysql, it is very simple.
代码如下  

//配置mysql数据库连接参数
$db = mysql_connect(www.45it.net, "user","password");
mysql_select_db("message",$db);
//在执行sql语句之前加上下面这一行
mysql_query("SET NAMES 'utf8'",$db);

The code is as follows

//Configure mysql database connection parameters
$db = mysql_connect(www.45it.net, "user","password");
mysql_select_db("message",$db);
//Add the following line before executing the sql statement
mysql_query("SET NAMES 'utf8'", $db);

If you submit again like this, you will find that even if your page is submitted by gbk, the data submitted will be saved successfully

ps: We must use uft8 for ajax, because ajax only supports uft8 mode to transmit data.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/928039.htmlTechArticleSolving the problem of php mysql query inserting Chinese garbled characters 1. The Chinese problem is actually that it has been uniformly encoded, otherwise it will be garbled 1. The encoding of database and PHP pages is unified 2. Database data table fields and pages...
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