search
Homephp教程php手册解决php mysql查询插入中文乱码问题

解决php mysql查询插入中文乱码问题

Jun 13, 2016 am 09:18 AM
mysqlphponeChineseGarbled charactersinsertInquiresolvequestion

解决php mysql查询插入中文乱码问题

   一.中文问题其实就是经统一编码了否则就会乱码

  1.数据库与php页面编码统一

  2.数据库数据表字段与页面编码统一

  如果做到上面两种就不存在中文乱码问题了,那么我们来看mysql中文乱码问题解决例子

  1.我的mysql表如下

  --

  -- 表的结构 `useradmin`

  --

代码如下  

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 ;

  上面所有的都是utf8编码了,这样我来测试一个中文

代码如下  

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

  我说可以正常进入也有人说肯定不行其实关键不在sql语句了,重点在于php连接mysql数据时的编码设置,如下。

  页面编辑为gbk

代码如下  

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

?>

  连接数据库之后提交肯定是乱码或保存不进行了,如果要解决这个mysql中文乱码问题很简单在查询处的

代码如下  

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

  这样再提交你会发现就算你的页面是gbk的提交的数据保存也会成功哦

  ps:对于ajax我们就一定要用uft8了,因为ajax只支持uft8模式传输数据。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.