您好,欢迎访问一九零五行业门户网

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数据库实例
复制代码 代码如下:
exec(insert into db_demo(name,content) values('title','content'))){
echo 插入成功!;
echo $pdo -> lastinsertid();
}
?>
复制代码 代码如下:
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); 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);
}
}
?>
http://www.bkjia.com/phpjc/630758.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/630758.htmltecharticle1.pdo简介 pdo(php教程 data object) 是php 5 中加入的东西,是php 5新加入的一个重大功能,因为在php 5以前的php4/php3都是一堆的数据库教程扩展来跟...
其它类似信息

推荐信息