无为清净楼资源网 Design By www.qnjia.com

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

<"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) "_blank" href="https://www.jb51.net/Special/192.htm">PHP基于pdo操作数据库技巧总结》、《php+Oracle数据库程序设计技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

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

标签:
PHP,PDO,数据库,操作类

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com