国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - PHP封裝的數(shù)據(jù)庫模型Model類完整示例【基于PDO】

PHP封裝的數(shù)據(jù)庫模型Model類完整示例【基于PDO】

2019-06-01 14:23小菜鳥有大夢(mèng)想服務(wù)器之家 PHP教程

這篇文章主要介紹了PHP封裝的數(shù)據(jù)庫模型Model類,結(jié)合實(shí)例形式分析了php基于PDO針對(duì)mysql數(shù)據(jù)庫常見增刪改查、統(tǒng)計(jì)、判斷等相關(guān)操作封裝與使用技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP封裝的數(shù)據(jù)庫模型Model類。分享給大家供大家參考,具體如下:

<?php
    //引入配置文件
    include "../Config/config.php";
    class Model extends PDO
    {
        protected $tableName = "";//存儲(chǔ)表名
        protected $sql = "";//存儲(chǔ)最后執(zhí)行的SQL語句
        protected $limit = "";//存儲(chǔ)limit條件
        protected $order = "";//存儲(chǔ)order排序條件
        protected $field = "*";//存儲(chǔ)要查詢的字段
        protected $where = "";//存儲(chǔ)where條件
        protected $allFields = [];//存儲(chǔ)當(dāng)前表的所有字段
        /**
         * 構(gòu)造方法 初始化
         * @param string $tableName 要操作的表名
         */
        public function __construct($tableName)
        {
            //連接數(shù)據(jù)庫
            parent::__construct('mysql:host='.HOST.';dbname='.DB.';charset=utf8;port='.PORT,USER,PWD);
            //存儲(chǔ)表名
            $this->tableName = PRE.$tableName;
            //獲取當(dāng)前數(shù)據(jù)表中有哪些字段
            $this->getFields();
        }
        /**
         * 獲取當(dāng)前表的所有字段
     * @return array 成功則返回一維數(shù)組字段
         */
        public function getFields()
        {
            //查看當(dāng)前表結(jié)構(gòu)
            $sql = "desc {$this->tableName}";
            $res = $this->query($sql);//返回pdo對(duì)象
          //var_dump($res);
            if ($res) {
                $arr = $res->fetchAll(2);
                //var_dump($arr);
                //從二維數(shù)組中取出指定下標(biāo)的列
                $this->allFields =    array_column($arr,"Field");
                return $this->allFields;
            } else {
                die("表名錯(cuò)誤");
            }
        }
        /**
         * 添加操作
         * @param array $data 要添加的數(shù)組
         * @return int 返回受影響行數(shù)
         */
        public function add($data)
        {
            //判斷是否是數(shù)組
            if (!is_array($data)) {
                    return $this;
            }
            //判斷是否全是非法字段
            if (empty($data)) {
                    die("非法字段");
            }
            //過濾非法字段
            foreach($data as $k => $v){
                if (!in_array($k,$this->allFields)) {
                    unset($data[$k]);
                }
            }
            //將數(shù)組中的鍵取出
            $keys = array_keys($data);
            //將數(shù)組中取出的鍵轉(zhuǎn)為字符串拼接
            $key = implode(",",$keys);
            //將數(shù)組中的值轉(zhuǎn)化為字符串拼接
            $value = implode("','",$data);
            //準(zhǔn)備SQL語句
            $sql = "insert into {$this->tableName} ({$key}) values('{$value}')";
            $this->sql = $sql;
            //執(zhí)行并發(fā)送SQL,返回受影響行數(shù)
            return (int)$this->exec($sql);
        }
        /**
         * 刪除操作
         * @param string $id 要?jiǎng)h除的id
         * @return int 返回受影響行數(shù)
         */
        public function delete($id="")
        {
            //判斷id是否存在
            if (empty($id)) {
                $where = $this->where;
            }else{
                $where = "where id={$id}";
            }
            $sql = "delete from {$this->tableName} {$where}";
      $this->sql = $sql;
            //執(zhí)行并發(fā)送SQL,返回受影響行數(shù)
            return (int)$this->exec($sql);
        }
    /**
     * 修改操作
     * @param array $data 要修改的數(shù)組
     * @return int 返回受影響行數(shù)
     */
    public function update($data)
    {
      //判斷是否是數(shù)組
      if (!is_array($data)){
        return $this;
      }
      //判斷是否全是非法字段
      if (empty($data)) {
        die('全是非法字段');
      }
      $str = "";
      //過濾非法字段
      foreach ($data as $k=>$v) {
        //字段為id時(shí),判斷id是否存在的
        if ($k == "id"){
          $this->where = "where id={$v}";
          unset($data[$k]);
          continue;
        }
        //若字段不為id,則過濾后再拼接成set字段
        if (in_array($k, $this->allFields)) {
          $str .= "{$k}='{$v}',";
        } else {
          unset($data[$k]);
        }
      }
      //判斷是否傳了條件
      if (empty($this->where)) {
        die('請(qǐng)傳入修改條件');
      }
      //去除右邊的,
      $str = rtrim($str, ',');
      $sql = "update {$this->tableName} set {$str} {$this->where}";
      //echo $sql;
      $this->sql = $sql;
      return (int)$this->exec($sql);
    }
    /**
     * 查詢多條數(shù)據(jù)
     * @return array 成功返回二維數(shù)組,失敗返回空數(shù)組
     */
        public function select()
    {
          $sql = "select {$this->field} from {$this->tableName} {$this->where} {$this->order} {$this->limit}";
      $this->sql = $sql;
      //執(zhí)行SQL,結(jié)果集是一個(gè)對(duì)象
          $res = $this->query($sql);
          //判斷是否查詢成功,
      if ($res){
        //成功返回二維數(shù)組
        return $res->fetchAll(2);
      }
      //失敗返回空數(shù)組
      return [];
    }
    /**
     * 查詢一條數(shù)組
     * @param string $id 要查詢的id
     * @return array 返回一條數(shù)據(jù)
     */
    public function find($id="")
    {
      //判斷是否存在id
      if (empty($id)){
        $where = $this->where;
      }else{
        $where = "where id={$id}";
      }
          $sql = "select {$this->field} from {$this->tableName} {$where} {$this->order} limit 1";
      $this->sql = $sql;
      //執(zhí)行sql,結(jié)果集為對(duì)象
      $res = $this->query($sql);
      //判斷是否查詢成功
      if ($res){
        //成功則返回一條數(shù)據(jù)(一維數(shù)組)
        $result = $res->fetchAll(2);
        return $result[0];
      }
      //失敗返回空數(shù)組
      return [];
    }
    /**
     * 統(tǒng)計(jì)總數(shù)目
     * @return int 返回總數(shù)
     */
    public function count()
    {
      $sql = "select count(*) as total from {$this->tableName} {$this->where} limit 1";
      $this->sql = $sql;
      //執(zhí)行SQL,結(jié)果集為對(duì)象
      $res = $this->query($sql);
      //處理結(jié)果集
      if ($res){
       $result = $res->fetchAll(2);
       //var_dump($result);
        return $result[0]["total"];
      }
      return 0;
    }
    /**
     * 設(shè)置要查詢的字段信息
     * @param string $field 要查詢的字段
     * @return object 返回自己,保證連貫操作
     */
    public function field($field)
    {
      //判斷字段是否存在
      if (empty($filed)){
        return $this;
      }
      $this->field = $field;
      return $this;
    }
    /**
     * 獲取最后執(zhí)行的sql語句
     * @return string sql語句
     */
    public function _sql()
    {
      return $this->sql;
    }
    /**
     * where條件
     * @param string $where 要輸入的where條件
     * @return object 返回自己,保證連貫操作
     */
    public function where($where)
    {
      $this->where = "where ".$where;
      return $this;
    }
    /**
     * order條件
     * @param string $order 要輸入的order條件
     * @return object 返回自己,保證連貫操作
     */
    public function order($order)
    {
      $this->order = "order by ".$order;
      return $this;
    }
    /**
     * limit條件
     * @param string $limit 要輸入的limit條件
     * @return object 返回自己,保證連貫操作
     */
    public function limit($limit)
    {
      $this->limit = "limit ".$limit;
      return $this;
    }
}

 

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 能直接看的av网站 | 亚洲视频在线观看 | 亚洲精品www久久久久久广东 | 福利片在线观看 | 亚洲伊人久久综合 | 在线免费观看视频 | 国产精品一区在线 | 亚洲国产精品成人女人久久久 | 亚洲乱码国产乱码精品精的特点 | 最新一级毛片 | 黄色午夜 | 亚洲视频在线播放 | 欧美三级影院 | 来个一级毛片 | 久久久久久久av | 国产精品18久久久久久首页狼 | 中文字幕久久精品 | 在线免费黄 | 91精品国产综合久久香蕉的用户体验 | 亚洲精品一二区 | 求av网址| 久久久久久夜 | 亚洲天堂久久 | 在线观看中文字幕 | 在线播放一区 | 国产精品久久久久久久免费大片 | 色啪网站 | 日本黄色大片免费看 | 中文字幕一区二区三区不卡 | 久久久精品欧美 | 夜夜夜久久久 | 无码一区二区三区视频 | 国产精品一二 | 亚洲91av | 久久精品亚洲一区二区 | 爱色av网站 | 91精品国产综合久久久久 | 亚洲激情在线视频 | 午夜在线小视频 | 亚洲精品在线免费看 | 国产欧美在线观看 |