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封装的PDO数据库操作类实例请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
PHP性能优化案例分享

PHP实现短信验证码的发送次数限制

PHP中的异常处理机制深入讲解

PHP常见七种算法合集代码实例

PHP微信扫描二维码关注公众号后自动登录第三方网站