pdo封装2

Posted lgl的守望地盘

tags:

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

<?php
//添加了一个 _createSql 方法,负责创建所有sql
class Db{
    static private $ins;
    private $pdo;
    private $table;
    private $where;
    private $field = ‘*‘;
    private $joinWhere = ‘where‘;
    private $order;
    private $limit;


    final private function __construct(){
        //从配置文件读入数据库信息
        $dsn  =  ‘mysql:dbname=wxguide;host=127.0.0.1‘ ;
        $user  =  ‘root‘;
        $password  =  ‘‘;

        try {
             $this->pdo  = new  PDO ( $dsn ,  $user ,  $password );
        } catch ( PDOException $e ) {
            die(‘Connection failed: ‘  .  $e -> getMessage ());
        }
    }

    final private function __clone(){}

    static public function table($table){
        if(!self::$ins instanceof self){
            self::$ins = new self();
        }
        self::$ins->table = $table;
        return self::$ins;
    }

    public function query($sql){
        return $this->pdo->query($sql);
    }

    public function exec($sql){
        return $this->pdo->exec($sql);
    }

    public function insert($data){
        $type = ‘insert‘;
        $sql = $this->_createSql($type,$data);
        return $this->exec($sql);
    }

    public function delete(){
        $type = ‘delete‘;
        $sql = $this->_createSql($type);
        return $this->exec($sql);
    }

    public function update($data){
        $type = ‘update‘;
        $sql = $this->_createSql($type,$data);
        return $this->exec($sql);
    }

    public function find(){
        $type = ‘select‘;
        $sql = $this->_createSql($type);
        return $this->query($sql)->fetch(PDO::FETCH_ASSOC);
    }

    public function select(){
        $type = ‘select‘;
        $sql = $this->_createSql($type);
        return $this->query($sql)->fetchAll(PDO::FETCH_ASSOC);
        
    }

    //条件字符串的【条件的值】需要用 引号(值如果是数字可以不用,但是用也没影响,建议不用),如: $str = ‘id = 1 and name = "王五" ‘
    public function where($str){
        $this->where = $str;
        return $this;
    }

    public function field($str){
        $this->field = $str;
        return $this;
    }

    public function limit($numstr){
        $this->limit = ‘limit ‘ . $numstr;
        return $this;
    }

    public function order($str){
        $this->order = ‘order by ‘ . $str;
        return $this;
    }

    public function group(){
        
    }

    public function having(){
        
    }

    //给表取别名
    public function alias($str){
        $this->table .= ‘ ‘ . $str;
        return $this;
    }

    //可以多表联查,join(‘tb1 b,tb2 c‘,‘a,id=b.id and a.id = c.id‘)
    //$join 可以取值 INNER,left或者right,默认值是 INNER
    public function join($alias,$where = null,$join = ‘INNER‘){
        $this->where .= $where;
        if($join != ‘INNER‘){
            $this->table .= ‘ ‘.$join . ‘ join ‘ . $alias;
            $this->joinWhere = ‘ON‘;
        }else{
            $this->table .= ‘,‘ . $alias;
        }
        return $this;
    }

    //创建sql
    private function _createSql($type,$data=null){
        switch ($type) {
            case ‘insert‘:
                $arr = $this->_cf($data);
                $sql = ‘insert into ‘ . $this->table . ‘ (‘ . $arr[‘kstr‘] . ‘) VALUES (‘ . $arr[‘vstr‘] . ‘)‘;
                break;

            case ‘delete‘:
                $sql = ‘delete from ‘ . $this->table . ‘ ‘.$this->joinWhere.‘ ‘ . $this->where;
                break;
            
            case ‘update‘:
                $arr = $this->_cf($data);
                $sql = ‘update ‘ . $this->table . ‘ set ‘. $arr[‘ustr‘] .‘ ‘.$this->joinWhere.‘ ‘ . $this->where;
                break;

            case ‘select‘:
                $sql = ‘select ‘. $this->field .‘ from ‘ . $this->table;
                if($this->where){
                    $sql = $sql . ‘ ‘.$this->joinWhere.‘ ‘ . $this->where;
                }
                $sql .= ‘ ‘.$this->order;
                $sql .= ‘ ‘. $this->limit;
                break;

            default:
                die(‘sql创建出现错误‘);
                break;
        }
        $this->_clear();
        return $sql;
    }

    //属性数据还原
    private function _clear(){
        $this->table = ‘‘;
        $this->where = ‘‘;
        $this->field = ‘*‘;
        $this->joinWhere = ‘where‘;
        $this->order = ‘‘;
        $this->limit = ‘‘;
    }

    //拆分 数组,组装数据
    private function _cf($data){
        $arr = [];
        $str = ‘‘;
        foreach($data as $k=>$v){
            $arr[‘k‘][] = $k;
            $arr[‘v‘][] = ‘"‘.$v.‘"‘;
            $str .= $k . ‘="‘ . $v . ‘",‘;
        }
        $arr[‘kstr‘] = implode(‘,‘,$arr[‘k‘]);
        $arr[‘vstr‘] = implode(‘,‘,$arr[‘v‘]);
        $arr[‘ustr‘] = rtrim($str, ",");
        return $arr;
    }
}

header(‘Content-type:text/html;charset=gbk‘);

$a = Db::table(‘model‘)->where(‘1‘)->select();
print_r($a);

 

以上是关于pdo封装2的主要内容,如果未能解决你的问题,请参考以下文章

object_PDO基础封装代码

pdo封装2

php PHP:PDO片段

php PHP:PDO片段

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装