PHP 基于PDO封装CURD,分页类封装

Posted MiraclesGG

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 基于PDO封装CURD,分页类封装相关的知识,希望对你有一定的参考价值。

<?php


class mysql_pdo
{

    private $_host;
    private $_user;
    private $_pwd;
    private $_dbname;
    private $_pdo;
    private $_fields;
    private $_values;
    private $_array;
    private $curr_page = 1;
    private $count_page;
    private $each_page = 10;
    private $field_page = ‘*‘;
    private static $connection = null;



    private function __construct ()
    {
        // 防止实例化
    }
    /*
        @@ 入口方法
        parameter null
    */

    public static function _interance ()
    {
        // 由于本地$pwd为空所有不判断$pwd;
        if (is_null(self::$connection)) self::$connection = new mysql_pdo();
        return self::$connection;
    }
    /*
        @@ 设置连接属性
        parameter $host 127.0.0.1, $user root, $pwd null, $dbname eatble.
    */

    public function _setdata ($host, $user, $pwd, $dbname)
    {
            $this->_host = $host;
            $this->_user = $user;
            $this->_pwd = $pwd;
            $this->_dbname = $dbname;
            $this->_connect();
    }

    /*
        @@ connection db
        parameter null.
    */

    public function _connect ()
    {
        try {
            $_pdo = new PDO(‘mysql:host=‘. $this->_host .‘;dbname=‘ .$this->_dbname, $this->_user, $this->_pwd);
            $_pdo->exec("SET NAMES utf8");
        } catch (PDOException $e) {
            die("Connection fail:" . $e->getMessage());
        }

        // 把$_pdo这个句柄放在静态属性,方便以下方法调用
        self::$connection = $_pdo;
    }

    /*
        @@ update data
        @@param $table 表格
        @@param $fields 字段名 关联数组
        @@param $where 条件
        return 受影响的行数
    */
    public function _update ($table, $fields, $where = null)
    {
        $fields_key = implode(array_keys($fields));
        $fields_value = implode(array_values($fields));
        $sql = "update {$table} set {$fields_key} = ‘{$fields_value}‘ " . ($where==null?null: "where " .$where);
        return self::$connection->exec($sql);
    }

    /*
        @@ insert data
        @@param $table 表格
        @@param $array 关联数组
    */

    public function _insert ($table, array $array)
    {
        $this->_array = $array;
        $this->foreachstr();
        $sql = "insert into {$table} ($this->_fields) values (‘{$this->_values}‘)";
        return self::$connection->exec($sql);
    }

    /*
        @@ select data
        @param $table 表格
        @param $field string field,field2,field3,
        @param $where array 一维关联数组 default null;
        @result_type  PDO::FETCH_BOTH 关联加索引 PDO::FETCH_NUM 索引 default PDO::FETCH_ASSOC 关联
    */

    public function _select ($table, $fields = ‘*‘, $where = null, $result_type = PDO::FETCH_ASSOC)
    {
        if ($fields == ‘*‘ and $where == null) {
            $sql = "select {$fields} from {$table} " . ($where==null?null:"where ".$where);
            $stmt = self::$connection->prepare($sql);
            $stmt->execute();
            while ($row = $stmt->fetch($result_type)) {
                $rows[] = $row;
            }
            return $rows;
        }
        if ($where != null) {
            if (!is_array($where)) return false;
            // 赋值给定义的属性交给以下foreacharray处理,返回的是一个字符串
            $this->_array = $where;
            $where = $this->foreacharray();
            if ($where) {
                $sql = "select {$fields} from {$table} ". ($where==null?null:"where ".$where);
                $stmt = self::$connection->prepare($sql);
                $stmt->execute();
                while ($row = $stmt->fetch($result_type)) {
                    $rows[] = $row;
                }
            }
            return $rows;
        }
    }
    /*
        @查询表格中数据的条数
        @param $table 表格
        @param $where 关联数组
        @return 查询到的数量 number
    */

    public function _selectnum ($table, $where = null)
    {
        $sql = ‘‘;
        if (is_null($where)) {
            $sql .= "select count(*) from {$table}";
        } else {
            $this->_array = $where;
            $where = $this->foreacharray();
            $sql .= "select count(*) from {$table} " .($where==null?null:"where ".$where);
        }
        $stmt = self::$connection->prepare($sql);
        $stmt->execute();
        $row_count = $stmt->fetch();
        return $row_count[0];
    }

    /*
        @@ delte data
        @$table 表格
        @$where array default null
        @return 受影响的行数
    */
    public function _delete ($table, $where = null)
    {
        if (is_null($where)) return false;      // 防止误删表
        if (!is_array($where)) return array();
        $this->_array = $where;
        $where = $this->foreacharray();
        $sql = "delete from {$table} " . ($where==null?null:"where " . $where);
        return self::$connection->exec($sql);
    }
    /*
        @@ 分割数组为字符串
        @@param $array 一维关联数组
        @$this->_fields field1, field2, field3.
        @$this->_values ‘values1‘, ‘values2‘, ‘values3‘;
    */


    protected function foreachstr ()
    {
        if (!is_array($this->_array)) return false;
        $fields = ‘‘;
        $values = ‘‘;
        foreach ($this->_array as $key => $val) {
             $fields .= $key.‘,‘;
             $values .= $val.‘,‘;
        }
        $this->_fields = rtrim($fields, ‘,‘);
        $this->_values = str_replace(‘,‘, ‘\‘,\‘‘, rtrim($values, ‘,‘));
    }

    /*
        @@ 将关联数组转换为带有and的字符串
        @@param $array 关联数组
    */
    protected function foreacharray ()
    {
        if (!is_array($this->_array)) return false;
        $str = ‘‘;
        foreach ($this->_array as $key => $val) {
            if ($key == $val) return false;         // 防止sql注入
            $str .= $key .‘ = ‘. "‘$val‘".‘ and ‘;
        }
        return substr($str, 0, strrpos($str, ‘and‘));
    }

    /*
        @@ 每页显示的数据
        @param $table 表格
        @param $field 要查询的字段  string field1, fiedl2, field3, default *
        @param $curr_page 当前页码
        @param $each_page 自定义每页数据 default 10
        @param $where 条件
        @return $page  array 关联数组
    */
    public function _showpage ($table, $field_page = null, $curr_page = null, $each_page = null, $where = null)
    {
        if (is_null($field_page)) $field_page = $this->field_page;
        $this->count_page = $this->_selectnum($table);      // 直接调用 _selectnum 拿到总条数
        if (is_null($each_page)) $each_page = $this->each_page;     // 如果$each_page 为空则使用默认的$each_page default 10
        // 防止输入错误的当前页导致当前页参数无效
        if (is_null($curr_page) || $curr_page > ceil($this->count_page/$each_page) || $curr_page <= 0) $curr_page = $this->curr_page;
        if (!is_null($where)) {
            $this->_array = $where;
            $where = $this->foreacharray();
        }
        $curr_page = ($curr_page-1)*$each_page;
        $count_page = ceil($this->count_page/$each_page);
        $sql = "select {$field_page} from {$table} " .($where==null?null:"where ". $where). " limit {$curr_page}, {$each_page}";  // sql注入会让这里多一个where
        $stmt = self::$connection->prepare($sql);
        $stmt->execute();
        $pages = [];
        while ($page = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $pages[] = $page;
        }
        $this->count_page = $count_page;
        if ($pages == null) return false;
        return $pages;

    }

    /*
        @下一页
        @param $table 表格
        @param $field_page 要查询的字段
        @param $curr_page 条件不符合时调用的是第一页的数据
        @return array 二维关联数组
    */

    public function _nextpage ($table, $field_page, $curr_page = null)
    {
        $curr_page<$this->count_page?$this->curr_page+=1:$this->curr_page=1;
        $result = $this->_showpage($table, $field_page, $curr_page);
        return $result;
    }

    /*
        @上一页
        @param $table 表格
        @param $field_page 要查询的字段
        @param $curr_page 默认为空时调用的是第一页的数据
        @return array 二维关联数组
    */

    public function _prevpage ($table, $field_page, $curr_page = null)
    {
        $result = $this->_showpage($table, $field_page, $curr_page);
        $curr_page>=0?$this->curr_page-=1:$this->curr_page=$this->count_page;
        return $result;
    }

    /*
        @总页数
        @ param $table 表格
        @ return 总页数
    */
    public function _showcount ($table)
    {
        $this->_showpage($table);
        return $this->count_page;
    }
}

 

以上是关于PHP 基于PDO封装CURD,分页类封装的主要内容,如果未能解决你的问题,请参考以下文章

自定义MVC框架之工具类-分页类的封装

封装的完美分页类

分页类封装

thinkphp5.1封装一个分页组件,tp5.1怎么封装分页组件

Laravel --实战篇 (自定义分页类)

封装:PDO与MySQL之间的无缝切换