欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP封装的PDO数据库操作类实例

程序员文章站 2024-03-11 11:11:49
本文实例讲述了php封装的pdo数据库操作类。分享给大家供大家参考,具体如下:

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

<?php
class databasehandler {
    /**
     * sql语句查询
     */
    public static function query_data ($dataname,$sql,$query=array()) {
      $result = array();
      if (!empty($sql)) {
        $data = bj_pdodb::factory($dataname)->allprepare($sql, $query);
        if (!$data->isempty()) {
          $result = $data->data;
        }
      }
      return $result;
    }
    /**
     * 查询数据
     */
    public static function select_data ($dataname,$tablename,$where,$param=array(),$desc='') {
      $result = array();
      $query = array();
      $query_str = '1=1';
      $tmp = '';
      $str_arr = '';
      if (!empty($param)) {
        $str = '';
        foreach ($param as $val) {
          $str .= '`'.$val.'`,';
        }
        $str = substr($str,0,-1);
      }
      $str_arr = !empty($str) ? $str : '*';
      if ( !empty($where) ) {
        foreach ($where as $key => $value) {
          $tmp .= ' and '.$key.'=:'.$key.'';
          $query[':'.$key] = $value;
        }
      }
      $query_str .= $tmp;
      if (!empty($desc)) {
        $query_str .= $desc;
      }
      $sql = "select {$str_arr} from `".$tablename."` where {$query_str} ";
      $query_data = bj_pdodb::factory($dataname)->allprepare($sql, $query);
      if ( !$query_data->isempty() ) {
        $result = $query_data->data;
      }
      return $result;
    }
    /**
     * 批量添加信息
     */
    public static function insert_data($dataname,$tablename,$add_param){
      $flag = false;
      if (!empty($add_param)) {
        $fileds = array();
        $str = '';
        foreach($add_param as $k=>$val){
          $str .= '(';
          $i = 0;
          foreach ($val as $key=>$vl) {
            $fileds[$i] = '`'.$key.'`';
            $str .= "'".addslashes($vl)."',";
            $i++;
          }
          $str = substr($str,0,-1);
          $str .= '),';
        }
        $filed_str = implode(',',$fileds);
        $val_str = substr($str,0,-1);
        $sql = "insert into `".$tablename."` ({$filed_str}) values {$val_str}";
        $ret = bj_pdodb::factory($dataname)->insertprepare($sql);
        if ( !$ret->isempty() ) {
            $insertid = $ret->data;
            if($insertid > 0){
              return $insertid;
            }
        }
      }
      return $flag;
    }
  /**
   * 删除数据
   */
  public static function delete_data ($dataname,$tablename,$id) {
    $query = array(':id' => $id);
    $sql = "delete from ". $tablename. " where `id`=:id";
    $db_res = bj_pdodb::factory($dataname)->simpleprepare($sql,$query);
    return $db_res->data;
  }
  /**
   * 更新数据
   */
  public static function update_data ($dataname,$tablename,$id,$array) {
    $flag = false;
    if ( !empty($array) && !empty($id)) {
      $query = array(':id' => $id);
      $up_str = '';
      foreach($array as $key=>$val) {
        $query[':'.$key] = addslashes($val);
        $up_str .= '`'.$key.'`=:'.$key.',';
      }
      $up_str = substr($up_str,0,-1);
      $sql = "update `".$tablename."` set ".$up_str." where `id`=:id";
      $db_res = bj_pdodb::factory($dataname)->changeprepare($sql, $query);
      $flag = true;
    }
    return $flag;
  }
  /**
   * 事务
   */
  public static function transactionhandler($dataname,$type){
    switch ($type) {
      case 'begin':
        return bj_pdodb::factory($dataname)->begintransaction();
        break;
      case 'commit':
        return bj_pdodb::factory($dataname)->commit();
        break;
      case 'rollback':
        return bj_pdodb::factory($dataname)->rollback();
        break;
      default :
        exit;
    }
  }
  /**
   * curl获取数据get
   */
  public static function curl_data ($curl_url){
    if (!empty($curl_url)) {
      $ch = curl_init();
      curl_setopt($ch, curlopt_url, $curl_url);
      curl_setopt($ch, curlopt_returntransfer, 1);
      curl_setopt($ch, curlopt_header, 0);
      $output = curl_exec($ch);
      curl_close($ch);
      return $output;
    }
    return false;
  }
  /**
   * curl提交数据
   */
  public static function curl_data_post ($ch_url,$param) {
    $result = array();
    if (!empty($ch_url) && !empty($param)) {
      $query_string = http_build_query($param);
      $ch = curl_init();
      curl_setopt($ch, curlopt_url, $ch_url);
      curl_setopt($ch, curlopt_returntransfer,1);
      curl_setopt($ch, curlopt_post,1);
      curl_setopt($ch, curlopt_postfields, $query_string);
      curl_setopt($ch, curlopt_httpheader,array("application/x-www-form-urlencoded; charset=utf-8"));
      $output = curl_exec($ch);
      curl_close($ch);
      $result = !empty($output) ? json_decode($output,true) : array();
    }
    return $result;
  }
}

更多关于php相关内容感兴趣的读者可查看本站专题:《php基于pdo操作数据库技巧总结》、《php+oracle数据库程序设计技巧总结》、《php+mongodb数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。