[PHP] PDO数据库操作类 →→→→→进入此内容的聊天室

来自 , 2021-03-19, 写在 PHP, 查看 114 次.
URL http://www.code666.cn/view/1ec3e7af
  1. /**
  2.  * 作者:胡睿
  3.  * 日期:2012/07/21
  4.  * 电邮:hooray0905@foxmail.com
  5.  */
  6.   
  7. class HRDB{
  8.     protected $pdo;
  9.     protected $res;
  10.     protected $config;
  11.      
  12.     /*构造函数*/
  13.     function __construct($config){
  14.         $this->Config = $config;
  15.         $this->connect();
  16.     }
  17.      
  18.     /*数据库连接*/
  19.     public function connect(){
  20.         $this->pdo = new PDO($this->Config['dsn'], $this->Config['name'], $this->Config['password']);
  21.         $this->pdo->query('set names utf8;');
  22.         //把结果序列化成stdClass
  23.         //$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
  24.         //自己写代码捕获Exception
  25.         $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  26.     }
  27.      
  28.     /*数据库关闭*/
  29.     public function close(){
  30.         $this->pdo = null;
  31.     }
  32.      
  33.     public function query($sql){
  34.         $res = $this->pdo->query($sql);
  35.         if($res){
  36.             $this->res = $res;
  37.         }
  38.     }
  39.     public function exec($sql){
  40.         $res = $this->pdo->exec($sql);
  41.         if($res){
  42.             $this->res = $res;
  43.         }
  44.     }
  45.     public function fetchAll(){
  46.         return $this->res->fetchAll();
  47.     }
  48.     public function fetch(){
  49.         return $this->res->fetch();
  50.     }
  51.     public function fetchColumn(){
  52.         return $this->res->fetchColumn();
  53.     }
  54.     public function lastInsertId(){
  55.         return $this->res->lastInsertId();
  56.     }
  57.      
  58.     /**
  59.      * 参数说明
  60.      * int              $debug      是否开启调试,开启则输出sql语句
  61.      *                              0   不开启
  62.      *                              1   开启
  63.      *                              2   开启并终止程序
  64.      * int              $mode       返回类型
  65.      *                              0   返回多条记录
  66.      *                              1   返回单条记录
  67.      *                              2   返回行数
  68.      * string/array     $table      数据库表,两种传值模式
  69.      *                              普通模式:
  70.      *                              'tb_member, tb_money'
  71.      *                              数组模式:
  72.      *                              array('tb_member', 'tb_money')
  73.      * string/array     $fields     需要查询的数据库字段,允许为空,默认为查找全部,两种传值模式
  74.      *                              普通模式:
  75.      *                              'username, password'
  76.      *                              数组模式:
  77.      *                              array('username', 'password')
  78.      * string/array     $sqlwhere   查询条件,允许为空,两种传值模式
  79.      *                              普通模式:
  80.      *                              'and type = 1 and username like "%os%"'
  81.      *                              数组模式:
  82.      *                              array('type = 1', 'username like "%os%"')
  83.      * string           $orderby    排序,默认为id倒序
  84.      */
  85.     public function select($debug, $mode, $table, $fields="*", $sqlwhere="", $orderby="tbid desc"){
  86.         //参数处理
  87.         if(is_array($table)){
  88.             $table = implode(', ', $table);
  89.         }
  90.         if(is_array($fields)){
  91.             $fields = implode(', ', $fields);
  92.         }
  93.         if(is_array($sqlwhere)){
  94.             $sqlwhere = ' and '.implode(' and ', $sqlwhere);
  95.         }
  96.         //数据库操作
  97.         if($debug === 0){
  98.             if($mode === 2){
  99.                 $this->query("select count(tbid) from $table where 1=1 $sqlwhere");
  100.                 $return = $this->fetchColumn();
  101.             }else if($mode === 1){
  102.                 $this->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
  103.                 $return = $this->fetch();
  104.             }else{
  105.                 $this->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
  106.                 $return = $this->fetchAll();
  107.             }
  108.             return $return;
  109.         }else{
  110.             if($mode === 2){
  111.                 echo "select count(tbid) from $table where 1=1 $sqlwhere";
  112.             }else if($mode === 1){
  113.                 echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
  114.             }
  115.             else{
  116.                 echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
  117.             }
  118.             if($debug === 2){
  119.                 exit;
  120.             }
  121.         }
  122.     }
  123.      
  124.     /**
  125.      * 参数说明
  126.      * int              $debug      是否开启调试,开启则输出sql语句
  127.      *                              0   不开启
  128.      *                              1   开启
  129.      *                              2   开启并终止程序
  130.      * int              $mode       返回类型
  131.      *                              0   无返回信息
  132.      *                              1   返回执行条目数
  133.      *                              2   返回最后一次插入记录的id
  134.      * string/array     $table      数据库表,两种传值模式
  135.      *                              普通模式:
  136.      *                              'tb_member, tb_money'
  137.      *                              数组模式:
  138.      *                              array('tb_member', 'tb_money')
  139.      * string/array     $set        需要插入的字段及内容,两种传值模式
  140.      *                              普通模式:
  141.      *                              'username = "test", type = 1, dt = now()'
  142.      *                              数组模式:
  143.      *                              array('username = "test"', 'type = 1', 'dt = now()')
  144.      */
  145.     public function insert($debug, $mode, $table, $set){
  146.         //参数处理
  147.         if(is_array($table)){
  148.             $table = implode(', ', $table);
  149.         }
  150.         if(is_array($set)){
  151.             $set = implode(', ', $set);
  152.         }
  153.         //数据库操作
  154.         if($debug === 0){
  155.             if($mode === 2){
  156.                 $this->query("insert into $table set $set");
  157.                 $return = $this->lastInsertId();
  158.             }else if($mode === 1){
  159.                 $this->exec("insert into $table set $set");
  160.                 $return = $this->res;
  161.             }else{
  162.                 $this->query("insert into $table set $set");
  163.                 $return = NULL;
  164.             }
  165.             return $return;
  166.         }else{
  167.             echo "insert into $table set $set";
  168.             if($debug === 2){
  169.                 exit;
  170.             }
  171.         }
  172.     }
  173.      
  174.     /**
  175.      * 参数说明
  176.      * int              $debug      是否开启调试,开启则输出sql语句
  177.      *                              0   不开启
  178.      *                              1   开启
  179.      *                              2   开启并终止程序
  180.      * int              $mode       返回类型
  181.      *                              0   无返回信息
  182.      *                              1   返回执行条目数
  183.      * string           $table      数据库表,两种传值模式
  184.      *                              普通模式:
  185.      *                              'tb_member, tb_money'
  186.      *                              数组模式:
  187.      *                              array('tb_member', 'tb_money')
  188.      * string/array     $set        需要更新的字段及内容,两种传值模式
  189.      *                              普通模式:
  190.      *                              'username = "test", type = 1, dt = now()'
  191.      *                              数组模式:
  192.      *                              array('username = "test"', 'type = 1', 'dt = now()')
  193.      * string/array     $sqlwhere   修改条件,允许为空,两种传值模式
  194.      *                              普通模式:
  195.      *                              'and type = 1 and username like "%os%"'
  196.      *                              数组模式:
  197.      *                              array('type = 1', 'username like "%os%"')
  198.      */
  199.     public function update($debug, $mode, $table, $set, $sqlwhere=""){
  200.         //参数处理
  201.         if(is_array($table)){
  202.             $table = implode(', ', $table);
  203.         }
  204.         if(is_array($set)){
  205.             $set = implode(', ', $set);
  206.         }
  207.         if(is_array($sqlwhere)){
  208.             $sqlwhere = ' and '.implode(' and ', $sqlwhere);
  209.         }
  210.         //数据库操作
  211.         if($debug === 0){
  212.             if($mode === 1){
  213.                 $this->exec("update $table set $set where 1=1 $sqlwhere");
  214.                 $return = $this->res;
  215.             }else{
  216.                 $this->query("update $table set $set where 1=1 $sqlwhere");
  217.                 $return = NULL;
  218.             }
  219.             return $return;
  220.         }else{
  221.             echo "update $table set $set where 1=1 $sqlwhere";
  222.             if($debug === 2){
  223.                 exit;
  224.             }
  225.         }
  226.     }
  227.      
  228.     /**
  229.      * 参数说明
  230.      * int              $debug      是否开启调试,开启则输出sql语句
  231.      *                              0   不开启
  232.      *                              1   开启
  233.      *                              2   开启并终止程序
  234.      * int              $mode       返回类型
  235.      *                              0   无返回信息
  236.      *                              1   返回执行条目数
  237.      * string           $table      数据库表
  238.      * string/array     $sqlwhere   删除条件,允许为空,两种传值模式
  239.      *                              普通模式:
  240.      *                              'and type = 1 and username like "%os%"'
  241.      *                              数组模式:
  242.      *                              array('type = 1', 'username like "%os%"')
  243.      */
  244.     public function delete($debug, $mode, $table, $sqlwhere=""){
  245.         //参数处理
  246.         if(is_array($sqlwhere)){
  247.             $sqlwhere = ' and '.implode(' and ', $sqlwhere);
  248.         }
  249.         //数据库操作
  250.         if($debug === 0){
  251.             if($mode === 1){
  252.                 $this->exec("delete from $table where 1=1 $sqlwhere");
  253.                 $return = $this->res;
  254.             }else{
  255.                 $this->query("delete from $table where 1=1 $sqlwhere");
  256.                 $return = NULL;
  257.             }
  258.             return $return;
  259.         }else{
  260.             echo "delete from $table where 1=1 $sqlwhere";
  261.             if($debug === 2){
  262.                 exit;
  263.             }
  264.         }
  265.     }
  266. }

回复 "PDO数据库操作类"

这儿你可以回复上面这条便签

captcha