首頁 > 後端開發 > php教程 > PHP封裝的PDO資料庫操作類別實例分析

PHP封裝的PDO資料庫操作類別實例分析

墨辰丷
發布: 2023-03-27 08:46:01
原創
1899 人瀏覽過

這篇文章主要介紹了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=&#39;&#39;) {
      $result = array();
      $query = array();
      $query_str = &#39;1=1&#39;;
      $tmp = &#39;&#39;;
      $str_arr = &#39;&#39;;
      if (!empty($param)) {
        $str = &#39;&#39;;
        foreach ($param as $val) {
          $str .= &#39;`&#39;.$val.&#39;`,&#39;;
        }
        $str = substr($str,0,-1);
      }
      $str_arr = !empty($str) ? $str : &#39;*&#39;;
      if ( !empty($where) ) {
        foreach ($where as $key => $value) {
          $tmp .= &#39; AND &#39;.$key.&#39;=:&#39;.$key.&#39;&#39;;
          $query[&#39;:&#39;.$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 = &#39;&#39;;
        foreach($add_param as $k=>$val){
          $str .= &#39;(&#39;;
          $i = 0;
          foreach ($val as $key=>$vl) {
            $fileds[$i] = &#39;`&#39;.$key.&#39;`&#39;;
            $str .= "&#39;".addslashes($vl)."&#39;,";
            $i++;
          }
          $str = substr($str,0,-1);
          $str .= &#39;),&#39;;
        }
        $filed_str = implode(&#39;,&#39;,$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(&#39;:id&#39; => $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(&#39;:id&#39; => $id);
      $up_str = &#39;&#39;;
      foreach($array as $key=>$val) {
        $query[&#39;:&#39;.$key] = addslashes($val);
        $up_str .= &#39;`&#39;.$key.&#39;`=:&#39;.$key.&#39;,&#39;;
      }
      $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 &#39;begin&#39;:
        return Bj_PdoDB::factory($dataName)->beginTransaction();
        break;
      case &#39;commit&#39;:
        return Bj_PdoDb::factory($dataName)->commit();
        break;
      case &#39;rollback&#39;:
        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 PDO封裝靜態類別程式碼分享 

php pdo封裝類別程式碼(支援事務) 

php pdo封裝類別實作mysql資料增刪除改 

##########################

以上是PHP封裝的PDO資料庫操作類別實例分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板