PHP基于pdo的数据库操作类【可支持mysql、sqlserver及oracle】
本文实例讲述了PHP基于pdo的数据库操作类。分享给大家供大家参考,具体如下:
工作中需要操作sqlserver、oracle都是使用的这个类,当时是在别人的基础上改进了,现在分享下
Config=$config;
$this->connect();
}
/*数据库连接*/
publicfunctionconnect(){
try{
$this->pdo=newPDO($this->Config['dsn'],$this->Config['username'],$this->Config['password']);//$dbh=newPDO('mysql:host=localhost;dbname=test',$user,$pass);
$this->pdo->query("setnamesutf8");
}catch(Exception$e){
echo'数据库连接失败,详情:'.$e->getMessage().'请在配置文件中数据库连接信息';
exit();
}
/*
if($this->Config['type']=='oracle'){
$this->pdo->query("setnames{$this->Config['charset']};");
}else{
$this->pdo->query("setnames{$this->Config['charset']};");
}
*/
//把结果序列化成stdClass
//$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_OBJ);
//自己写代码捕获Exception
//$this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);//属性名属性值数组以关联数组返回
}
/*数据库关闭*/
publicfunctionclose(){
$this->pdo=null;
}
//用于有记录结果返回的操作,特别是SELECT操作
publicfunctionquery($sql,$return=false){
$res=$this->pdo->query($sql);
if($res){
$this->res=$res;//未返回return$this->res;
}
if($return){
return$res;
}
}
//主要是针对没有结果集合返回的操作,比如INSERT、UPDATE、DELETE等操作
publicfunctionexec($sql,$return=false){
$res=$this->pdo->exec($sql);
if($res){
$this->res=$res;
}
if($return){//返回操作是否成功成功返回1失败0
return$res;
}
}
//将$this->res以数组返回(全部返回)
publicfunctionfetchAll(){
return$this->res->fetchAll();
}
//将$this->res以数组返回(一条记录)
publicfunctionfetch(){
return$this->res->fetch();
}
//返回所有字段
publicfunctionfetchColumn(){
return$this->res->fetchColumn();
}
//返回最后插入的id
publicfunctionlastInsertId(){
return$this->res->lastInsertId();
}
//返回最后插入的id
publicfunctionlastInsertId2(){
return$this->pdo->lastInsertId();
}
/**
*参数说明
*string/array$table数据库表,两种传值模式
*普通模式:
*'tb_member,tb_money'
*数组模式:
*array('tb_member','tb_money')
*string/array$fields需要查询的数据库字段,允许为空,默认为查找全部,两种传值模式
*普通模式:
*'username,password'
*数组模式:
*array('username','password')
*string/array$sqlwhere查询条件,允许为空,两种传值模式
*普通模式(必须加上and,$sqlwhere为空1=1正常查询):
*'andtype=1andusernamelike"%os%"'
*数组模式:
*array('type=1','usernamelike"%os%"')
*string$orderby排序,默认为id倒序
*int$debug是否开启调试,开启则输出sql语句
*0不开启
*1开启
*2开启并终止程序
*int$mode返回类型
*0返回多条记录
*1返回单条记录
*2返回行数
*/
publicfunctionselect($table,$fields="*",$sqlwhere="",$orderby="",$debug=0,$mode=0){
//参数处理
if(is_array($table)){
$table=implode(',',$table);
}
if(is_array($fields)){
$fields=implode(',',$fields);
/*
if($this->Config['type']=='oracle'){
//$fields=implode(',',$fields);//CUSTOMER_ID,FIRST_NAME,LAST_NAME,EMAIL
//$fields=implode(",'UTF8','ZHS16GBK'),convert(",$fields);
//$fields="convert(".$fields.",'UTF8','ZHS16GBK')";
}else{
$fields=implode(',',$fields);
}
*/
}
if(is_array($sqlwhere)){
$sqlwhere='and'.implode('and',$sqlwhere);
}
//数据库操作
if($debug===0){
if($mode===2){//统计
$this->query("selectcount(*)from$tablewhere1=1$sqlwhere");
$return=$this->fetchColumn();
}elseif($mode===1){//返回一条
$this->query("select$fieldsfrom$tablewhere1=1$sqlwhere$orderby");
$return=$this->fetch();
}else{
$this->query("select$fieldsfrom$tablewhere1=1$sqlwhere$orderby");
$return=$this->fetchAll();//如果$this->res为空即sql语句错误会提示CalltoamemberfunctionfetchAll()onanon-object
}
return$return;
}else{
if($mode===2){
echo"selectcount(*)from$tablewhere1=1$sqlwhere";
}elseif($mode===1){
echo"select$fieldsfrom$tablewhere1=1$sqlwhere$orderby";
}else{
echo"select$fieldsfrom$tablewhere1=1$sqlwhere$orderby";
}
if($debug===2){
exit;
}
}
}
/**
*参数说明
*string/array$table数据库表,两种传值模式
*普通模式:
*'tb_member,tb_money'
*数组模式:
*array('tb_member','tb_money')
*string/array$set需要插入的字段及内容,两种传值模式
*普通模式:
*'username="test",type=1,dt=now()'
*数组模式:
*array('username="test"','type=1','dt=now()')
*int$debug是否开启调试,开启则输出sql语句
*0不开启
*1开启
*2开启并终止程序
*int$mode返回类型
*0无返回信息
*1返回执行条目数
*2返回最后一次插入记录的id
*/
publicfunctionoic_insert($table,$set,$debug=0,$mode=0){
//参数处理
if(is_array($table)){
$table=implode(',',$table);
}
if(is_array($set)){
$s='';$i=0;
foreach($setas$k=>$v){
$i++;
$s[$i]=$k;//,连接
$val[$i]=$v;
}
$sarr=implode(",",$s);//去掉最后一个,
//array_pop($sarr);
$set=implode("','",$val);////15221579236','张三','','2001','8','4','女','是
//$set=implode(',',$set);
}
//数据库操作
if($debug===0){
if($mode===2){
$this->query("insertinto$table($sarr)values('".$set."')");
//$return=$this->lastInsertId();
}elseif($mode===1){
$this->exec("insertinto$table($sarr)values('".$set."')");
$return=$this->res;
}else{
$this->query("insertinto$table($sarr)values('".$set."')");
$return=NULL;
}
return$return;
}else{
echo"insertinto$table($sarr)values('".$set."')";
if($debug===2){
exit;
}
}
}
publicfunctioninsert($table,$set,$debug=0,$mode=0){
//参数处理
if(is_array($table)){
$table=implode(',',$table);
}
if(is_array($set)){
$s='';
foreach($setas$k=>$v){
$s.=$k."='".$v."',";//,连接
}
$sarr=explode(',',$s);//去掉最后一个,
array_pop($sarr);
$set=implode(',',$sarr);
//$set=implode(',',$set);
}
//数据库操作
if($debug===0){
if($mode===2){
$this->query("insertinto$tableset$set");
$return=$this->pdo->lastInsertId();
}elseif($mode===1){
$this->exec("insertinto$tableset$set");
$return=$this->res;
}else{
$this->query("insertinto$tableset$set");
$return=NULL;
}
return$return;
}else{
echo"insertinto$tableset$set";
if($debug===2){
exit;
}
}
}
/**
*参数说明
*string$table数据库表,两种传值模式
*普通模式:
*'tb_member,tb_money'
*数组模式:
*array('tb_member','tb_money')
*string/array$set需要更新的字段及内容,两种传值模式
*普通模式:
*'username="test",type=1,dt=now()'
*数组模式:
*array('username="test"','type=1','dt=now()')
*string/array$sqlwhere修改条件,允许为空,两种传值模式
*普通模式:
*'andtype=1andusernamelike"%os%"'
*数组模式:
*array('type=1','usernamelike"%os%"')
*int$debug是否开启调试,开启则输出sql语句
*0不开启
*1开启
*2开启并终止程序
*int$mode返回类型
*0无返回信息
*1返回执行条目数
*/
publicfunctionupdate($table,$set,$sqlwhere="",$debug=0,$mode=0){
//参数处理
if(is_array($table)){
$table=implode(',',$table);
}
if(is_array($set)){
$s='';
foreach($setas$k=>$v){
$s.=$k."='".$v."',";
}
$sarr=explode(',',$s);//去掉最后一个,
array_pop($sarr);
$set=implode(',',$sarr);
//$set=implode(',',$set);
}
if(is_array($sqlwhere)){
$sqlwhere='and'.implode('and',$sqlwhere);
}
//数据库操作
if($debug===0){
if($mode===1){
$this->exec("update$tableset$setwhere1=1$sqlwhere");
$return=$this->res;
}else{
$this->query("update$tableset$setwhere1=1$sqlwhere");
$return=true;
}
return$return;
}else{
echo"update$tableset$setwhere1=1$sqlwhere";
if($debug===2){
exit;
}
}
}
/**
*参数说明
*string$table数据库表
*string/array$sqlwhere删除条件,允许为空,两种传值模式
*普通模式:
*'andtype=1andusernamelike"%os%"'
*数组模式:
*array('type=1','usernamelike"%os%"')
*int$debug是否开启调试,开启则输出sql语句
*0不开启
*1开启
*2开启并终止程序
*int$mode返回类型
*0无返回信息
*1返回执行条目数
*/
publicfunctiondelete($table,$sqlwhere="",$debug=0,$mode=0){
//参数处理
if(is_array($sqlwhere)){
$sqlwhere='and'.implode('and',$sqlwhere);//是字符串需自己加上and
}
//数据库操作
if($debug===0){
if($mode===1){
$this->exec("deletefrom$tablewhere1=1$sqlwhere");
$return=$this->res;
}else{
$this->query("deletefrom$tablewhere1=1$sqlwhere");
$return=NULL;
}
return$return;
}else{
echo"deletefrom$tablewhere1=1$sqlwhere";
if($debug===2){
exit;
}
}
}
}
/*
sqlserver配置extension=php_pdo_mssql.dll和extension=php_pdo_sqlsrv.dll安装对应的ntwdblib.dll
http://msdn.microsoft.com/en-us/library/cc296170.aspx下载php版本对应的sqlsrv扩展
sqlserver配置odbc连接需开启extension=php_pdo_odbc.dll
*/
$mssql2008_config=array(
'dsn'=>'odbc:Driver={SQLServer};Server=192.168.1.60;Database=his',//数据库服务器地址
'username'=>'sa',
'password'=>'xxxxx',
);
$mssql=newPdodb($mssql2008_config);
$sql="select*from
(
selectrow_number()over(orderbytempcolumn)temprownumber,*
from(
selecttop10tempcolumn=0,a.*
fromDA_GR_HBFSa
where1=1
)t
)tt
wheretemprownumber>0";
$mssql->query($sql);
while($res=$mssql->fetch()){
$data[]=$res;
}
print_r($data);exit;
//mysql操作
$msyql_config=array(
'dsn'=>'mysql:host=localhost;dbname=talk',
'username'=>'root',
'password'=>'123456'
);
$mysql=newPDO_DB($msyql_config);
$sql='SELECTuser_id,user_name,nicknameFROMet_users';
$mysql->query($sql);
$data=$mysql->fetchAll();
print_r($data);exit;
//oracle操作
$oci_config=array(
'dsn'=>'oci:dbname=orcl',
'username'=>'BAOCRM',
'password'=>'BAOCRM'
);
$oracle=newPDO_DB($oci_config);
//print_r($oracle);exit;//PDO_DBObject([pdo:protected]=>PDOObject()[res:protected]=>[config:protected]=>[Config]=>Array([dsn]=>oci:dbname=orcl[name]=>PWACRM[password]=>PWACRM))
$sql="select*fromCUSTOMER_LEVELt";
$oracle->query($sql);
$data=$oracle->fetchAll();
print_r($data);exit;
/*
Array
(
[0]=>Array
(
[LEVEL_ID]=>1
[0]=>1
[LEVEL_NAME]=>普通会员
[1]=>普通会员
[LEVEL_DETAIL]=>普通会员
[2]=>普通会员
[SORT_NUMBER]=>15
[3]=>15
[CREATE_TIME]=>12-7月-12
[4]=>12-7月-12
[CREATE_BY]=>1
[5]=>1
[UPDATE_TIME]=>12-7月-12
[6]=>12-7月-12
[UPDATE_BY]=>1
[7]=>1
[STATE]=>正常
[8]=>正常
)
)*/
?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP基于pdo操作数据库技巧总结》、《php+Oracle数据库程序设计技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。