php pdo插入 中文乱码,pdo连接数据类与中文乱码解决方法_PHP教程

1.pdo简介

pdo(php教程 data object) 是php 5 中加入的东西,是php 5新加入的一个重大功能,因为在php 5以前的php4/php3都是一堆的数据库教程扩展来跟各个数据库的连接和处理,什么 php_mysql教程.dll、php_pgsql.dll、php_mssql.dll、php_sqlite.dll等等。

php6中也将默认使用pdo的方式连接,mysql扩展将被作为辅助

2.pdo配置

php.ini中,去掉”extension=php_pdo.dll”前面的”;”号,若要连接数据库,还需要去掉与pdo相关的数据库扩展前面的”;”号,然后重启apache服务器即可。

extension=php_pdo.dll

extension=php_pdo_mysql.dll

extension=php_pdo_pgsql.dll

extension=php_pdo_sqlite.dll

extension=php_pdo_mssql.dll

extension=php_pdo_odbc.dll

extension=php_pdo_firebird.dll

……

3.pdo连接mysql数据库

new pdo(“mysql:host=localhost;dbname=db_demo”,”root”,””);

默认不是长连接,若要使用数据库长连接,需要在最后加如下参数:

new pdo(“mysql:host=localhost;dbname=db_demo”,”root”,””,”array(pdo::attr_persistent => true) “);

4.pdo常用方法及其应用

pdo::query() 主要是用于有记录结果返回的操作,特别是select操作

pdo::exec() 主要是针对没有结果集合返回的操作,如insert、update等操作

pdo::lastinsertid() 返回上次插入操作,主键列类型是自增的最后的自增id

pdostatement::fetch() 是用来获取一条记录

pdostatement::fetchall() 是获取所有记录集到一个中

5.pdo操作mysql数据库实例

复制代码 代码如下:

$pdo = new pdo(“mysql:host=localhost;dbname=db_demo”,”root”,””);

if($pdo -> exec(“insert into db_demo(name,content) values(‘title’,’content’)”)){

echo “插入成功!”;

echo $pdo -> lastinsertid();

}

?>

复制代码 代码如下:

$pdo = new pdo(“mysql:host=localhost;dbname=db_demo”,”root”,””);

$rs = $pdo -> query(“select * from test”);

while($row = $rs -> fetch()){

print_r($row);

}

?>

网上最常出现的解决中文乱码显示的代码是:

第一种:pdo::__construct($dsn, $user, $pass, array

(pdo::mysql_attr_init_command => “set names’utf8′;”));

我试过用第一种方法,可结果是,name字段只显示一个‘c’字符。之后的本该显示中文的地方却是空白。

结果是这样的:如图1示

我是只要解决的:直接将utf8替换成了gbk,就可以了,即:

pdo::__construct($dsn, $user, $pass, array(pdo::mysql_attr_init_command => “set

names’gbk’;”));

效果图2如下:

第二种:pdo::__construct($dsn, $user, $pass);

pdo::exec(“set names ‘utf8’;”);

第二种我也在我的环境里测试过,显示效果如图1所示,碰到这种情况,把utf8替换成gbk,就能显

示了。另外,这里的pdo::在使用的时候用$pdo->代替,当然,这个是个变量,变量名称可以自己定义。

第三种

:$pdo->query(‘set names utf8;’);

至于第三种呢,看了上面两种,应该也知道要吧utf8替换成gbk,也能正确显示了。

这几种我都测试过了。都行。哈哈。另外,我在这里还介绍一种解决中文乱码的一种方法,不过大同小异,

基本和第三种没什么却别,不通的是,这种方法,没用query而是用exec,代码如下:

$pdo->exec(“set character set gbk”);

/*

常用数据库操作,如:增删改查,获取单条记录、多条记录,返回最新一条插入记录id,返回操作记录行数等

*/

/*

参数说明

int $debug 是否开启调试,开启则输出sql语句

int $getcount 是否记数,返回值为行数

int $getrow 是否返回值单条记录

string $table 数据库表

string $fields 需要查询的数据库字段,允许为空,默认为查找全部

string $sqlwhere 查询条件,允许为空

string $orderby 排序,允许为空,默认为id倒序

*/

function hrselect($debug, $getcount, $getrow, $table, $fields=”*”, $sqlwhere=””, $orderby=”id desc”){

global $pdo;

if($debug){

if($getcount){

echo “select count(*) from $table where 1=1 $sqlwhere order by $orderby”;

}else{

echo “select $fields from $table where 1=1 $sqlwhere order by $orderby”;

}

exit;

}else{

if($getcount){

$rs = $pdo->query(“select count(*) from $table where 1=1 $sqlwhere order by $orderby”);

return $rs->fetchcolumn();

}elseif($getrow){

$rs = $pdo->query(“select $fields from $table where 1=1 $sqlwhere order by $orderby”);

return $rs->fetch();

}else{

$rs = $pdo->query(“select $fields from $table where 1=1 $sqlwhere order by $orderby”);

return $rs->fetchall();

}

}

}

/*

参数说明

int $debug 是否开启调试,开启则输出sql语句

int $execrow 是否开启返回执行条目数

int $lastinsertid 是否开启返回最后一条插入记录id

string $table 数据库表

string $fields 需要插入数据库的字段

string $values 需要插入数据库的信息,必须与$fields一一对应

*/

function hrinsert($debug, $execrow, $lastinsertid, $table, $fields, $values){

global $pdo;

if($debug){

echo “insert into $table ($fields) values ($values)”;

exit;

}elseif($execrow){

return $pdo->exec(“insert into $table ($fields) values ($values)”);

}elseif($lastinsertid){

return $pdo->lastinsertid(“insert into $table ($fields) values ($values)”);

}else{

$pdo->query(“insert into $table ($fields) values ($values)”);

}

}

/*

参数说明

int $debug 是否开启调试,开启则输出sql语句

int $execrow 是否开启执行并返回条目数

string $table 数据库表

string $set 需要更新的字段及内容,格式:a=’abc’,b=2,c=’2010-10-10 10:10:10′

string $sqlwhere 修改条件,允许为空

*/

function hrupdate($debug, $execrow, $table, $set, $sqlwhere=””){

global $pdo;

if($debug){

echo “update $table set $set where 1=1 $sqlwhere”;

exit;

}elseif($execrow){

return $pdo->exec(“update $table set $set where 1=1 $sqlwhere”);

}else{

$pdo->query(“update $table set $set where 1=1 $sqlwhere”);

}

}

/*

参数说明

int $debug 是否开启调试,开启则输出sql语句

int $execrow 是否开启返回执行条目数

string $table 数据库表

string $sqlwhere 删除条件,允许为空

*/

function hrdelete($debug, $execrow, $table, $sqlwhere=””){

global $pdo;

if($debug){

echo “delete from $table where 1=1 $sqlwhere”;

exit;

}elseif($execrow){

return $pdo->exec(“delete from $table where 1=1 $sqlwhere”);

}else{

$pdo->query(“delete from $table where 1=1 $sqlwhere”);

}

}

?>

www.bkjia.comtrueTechArticle1.pdo简介 pdo(php教程 data object) 是php 5 中加入的东西,是php 5新加入的一个重大功能,因为在php 5以前的php4/php3都是一堆的数据库教程扩展来跟…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值