侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PHP基于pdo的数据库操作类【可支持mysql、sqlserver及oracle】

数据库  /  管理员 发布于 7年前   192

本文实例讲述了PHP基于pdo的数据库操作类。分享给大家供大家参考,具体如下:

工作中需要操作sqlserver、oracle都是使用的这个类,当时是在别人的基础上改进了,现在分享下

Config = $config;    $this->connect();  }  /*数据库连接*/  public function connect(){    try {       $this->pdo= new PDO($this->Config['dsn'], $this->Config['username'], $this->Config['password']);//$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);       $this->pdo->query("set names utf8");    }catch(Exception $e){      echo '数据库连接失败,详情: ' . $e->getMessage () . ' 请在配置文件中数据库连接信息';      exit ();    }    /*    if($this->Config['type']=='oracle'){      $this->pdo->query("set names {$this->Config['charset']};");    }else{      $this->pdo->query("set names {$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);//属性名 属性值 数组以关联数组返回  }  /*数据库关闭*/  public function close(){    $this->pdo = null;  }  //用于有记录结果返回的操作,特别是SELECT操作  public function query($sql,$return=false){    $res = $this->pdo->query($sql);    if($res){      $this->res = $res; // 未返回 return $this->res;    }    if($return){      return $res;    }  }  //主要是针对没有结果集合返回的操作,比如INSERT、UPDATE、DELETE等操作  public function exec($sql,$return=false){    $res = $this->pdo->exec($sql);    if($res){      $this->res = $res;    }    if($return){//返回操作是否成功 成功返回1 失败0      return $res;    }  }  //将$this->res以数组返回(全部返回)  public function fetchAll(){    return $this->res->fetchAll();  }  //将$this->res以数组返回(一条记录)  public function fetch(){    return $this->res->fetch();  }  //返回所有字段  public function fetchColumn(){    return $this->res->fetchColumn();  }  //返回最后插入的id  public function lastInsertId(){    return $this->res->lastInsertId();  }  //返回最后插入的id  public function lastInsertId2(){    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 正常查询):  * 'and type = 1 and username like "%os%"'  * 数组模式:  * array('type = 1', 'username like "%os%"')  * string $orderby 排序,默认为id倒序  *int $debug 是否开启调试,开启则输出sql语句  * 0 不开启  * 1 开启  * 2 开启并终止程序  * int $mode 返回类型  * 0 返回多条记录  * 1 返回单条记录  * 2 返回行数  */  public function select($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("select count(*) from $table where 1=1 $sqlwhere");        $return = $this->fetchColumn();      }else if($mode === 1){ //返回一条        $this->query("select $fields from $table where 1=1 $sqlwhere $orderby");        $return = $this->fetch();      }else{        $this->query("select $fields from $table where 1=1 $sqlwhere $orderby");        $return = $this->fetchAll();//如果 $this->res为空即sql语句错误 会提示Call to a member function fetchAll() on a non-object      }      return $return;    }else{        if($mode === 2){          echo "select count(*) from $table where 1=1 $sqlwhere";        }else if($mode === 1){          echo "select $fields from $table where 1=1 $sqlwhere $orderby";        }else{          echo "select $fields from $table where 1=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  */  public function oic_insert($table, $set, $debug=0, $mode=0){    //参数处理    if(is_array($table)){      $table = implode(', ', $table);    }    if(is_array($set)){      $s='';$i=0;      foreach($set as $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("insert into $table ($sarr) values('".$set."')");        //$return = $this->lastInsertId();      }else if($mode === 1){        $this->exec("insert into $table ($sarr) values('".$set."')");        $return = $this->res;      }else{        $this->query("insert into $table ($sarr) values('".$set."')");        $return = NULL;      }      return $return;    }else{      echo "insert into $table ($sarr) values('".$set."')";      if($debug === 2){        exit;      }    }  }  public function insert($table, $set, $debug=0, $mode=0){    //参数处理    if(is_array($table)){      $table = implode(', ', $table);    }    if(is_array($set)){      $s='';      foreach($set as $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("insert into $table set $set");        $return = $this->pdo->lastInsertId();      }else if($mode === 1){        $this->exec("insert into $table set $set");        $return = $this->res;      }else{        $this->query("insert into $table set $set");        $return = NULL;      }      return $return;    }else{      echo "insert into $table set $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 修改条件,允许为空,两种传值模式  * 普通模式:  * 'and type = 1 and username like "%os%"'  * 数组模式:  * array('type = 1', 'username like "%os%"')  * int $debug 是否开启调试,开启则输出sql语句  * 0 不开启  * 1 开启  * 2 开启并终止程序  * int $mode 返回类型  * 0 无返回信息  * 1 返回执行条目数  */  public function update($table, $set, $sqlwhere="", $debug=0, $mode=0){    //参数处理    if(is_array($table)){      $table = implode(', ', $table);    }    if(is_array($set)){      $s='';      foreach($set as $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 $table set $set where 1=1 $sqlwhere");        $return = $this->res;      }else{        $this->query("update $table set $set where 1=1 $sqlwhere");        $return = true;      }      return $return;    }else{      echo "update $table set $set where 1=1 $sqlwhere";      if($debug === 2){        exit;      }    }  }  /**  * 参数说明  * string $table 数据库表  * string/array $sqlwhere 删除条件,允许为空,两种传值模式  * 普通模式:  * 'and type = 1 and username like "%os%"'  * 数组模式:  * array('type = 1', 'username like "%os%"')  * int $debug 是否开启调试,开启则输出sql语句  * 0 不开启  * 1 开启  * 2 开启并终止程序  * int $mode 返回类型  * 0 无返回信息  * 1 返回执行条目数  */  public function delete($table, $sqlwhere="", $debug=0, $mode=0){    //参数处理    if(is_array($sqlwhere)){      $sqlwhere = ' and '.implode(' and ', $sqlwhere); //是字符串需自己加上and    }    //数据库操作    if($debug === 0){      if($mode === 1){        $this->exec("delete from $table where 1=1 $sqlwhere");        $return = $this->res;      }else{        $this->query("delete from $table where 1=1 $sqlwhere");        $return = NULL;      }      return $return;    }else{      echo "delete from $table where 1=1 $sqlwhere";      if($debug === 2){        exit;      }    }  }}/*sqlserver 配置 extension=php_pdo_mssql.dll和extension=php_pdo_sqlsrv.dll 安装对应的 ntwdblib.dllhttp://msdn.microsoft.com/en-us/library/cc296170.aspx 下载php版本对应的sqlsrv扩展sqlserver 配置 odbc连接需开启extension=php_pdo_odbc.dll*/$mssql2008_config=array(  'dsn'=>'odbc:Driver={SQL Server};Server=192.168.1.60;Database=his',//数据库服务器地址  'username'=>'sa',  'password'=>'xxxxx',);$mssql=new Pdodb($mssql2008_config);$sql="select * from(  select row_number()over(order by tempcolumn)temprownumber,*    from (      select top 10 tempcolumn=0,a.*      from DA_GR_HBFS a      where 1=1    ) t) ttwhere temprownumber>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=new PDO_DB($msyql_config);$sql = 'SELECT user_id, user_name, nickname FROM et_users ';$mysql->query($sql);$data=$mysql->fetchAll();print_r($data);exit;//oracle 操作$oci_config=array(  'dsn'=>'oci:dbname=orcl',  'username'=>'BAOCRM',  'password'=>'BAOCRM');$oracle=new PDO_DB($oci_config);//print_r($oracle);exit;//PDO_DB Object ( [pdo:protected] => PDO Object ( ) [res:protected] => [config:protected] => [Config] => Array ( [dsn] => oci:dbname=orcl [name] => PWACRM [password] => PWACRM ) )$sql="select * from CUSTOMER_LEVEL t";$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程序设计有所帮助。

您可能感兴趣的文章:

  • 防止MySQL注入或HTML表单滥用的PHP程序
  • PHP MYSQL注入攻击需要预防7个要点
  • PHP+mysql防止SQL注入的方法小结
  • php mysql_real_escape_string addslashes及mysql绑定参数防SQL注入攻击
  • Php中用PDO查询Mysql来避免SQL注入风险的方法
  • PHP连接MySQL数据库的三种方式实例分析【mysql、mysqli、pdo】
  • php中mysql连接方式PDO使用详解
  • php中数据库连接方式pdo和mysqli对比分析
  • php基于PDO实现功能强大的MYSQL封装类实例
  • PHP使用PDO创建MySQL数据库、表及插入多条数据操作示例
  • php使用mysqli和pdo扩展,测试对比mysql数据库的执行效率完整示例
  • PHP使用PDO实现mysql防注入功能详解


  • 上一条:
    数据库对象的同义词和序列
    下一条:
    Mysql和网页显示乱码解决方法集锦
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客