PDO封装

class Db {
    private static $_instance;
    private static  $conn = null;
    private function __construct()
    {
        //链接数据库
        self::getConn();
    }

    //链接数据库
    private static  function getConn(){
        if(self::$conn === null){
            self::$conn = new PDO("mysql:host=119.23.155.8;dbname=tianxi","root","2a4979bd1b8f371e");
            // self::$conn = new PDO("mysql:host=127.0.0.1;Database=SiteMIS","sa","1211");
            //设置编码
            self::$conn->exec('set names utf8');
        }
        return self::$conn;
    }

    private static function getInstance()
    {
        if(! (self::$_instance instanceof self) ) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }


    //查询多条语句的封装函数
    public static function getMore($field = '*', $table, $where = 1, $order = 'order by id asc', $offset = 0, $perpage = 1000000) {
        self::getInstance();
        $sql = "select $field from `$table` where $where $order limit $offset,$perpage";
        $query = self::getConn()->query($sql); //执行上面的语句
        $list = array(); //新建一个数组
        while($row = $query->fetch(PDO::FETCH_ASSOC)) {//循环news表
            $list[] = $row; //把执行出来的每一条内容赋值给list这个数组
        }
        return $list;
    }
    //获取列表
    public static function select($sql) {
        self::getInstance();
        $query = self::getConn()->query($sql); //执行上面的语句
        $list = array(); //新建一个数组
        while ($row = $query) {//循环news表
            $list[] = $row; //把执行出来的每一条内容赋值给list这个数组
        }
        return $list;
    }


    //获取列表
    public static function query($sql) {
        self::getInstance();
        $query = self::getConn()->query($sql); //执行上面的语句
        $row = $query->fetchAll(PDO::FETCH_ASSOC);
        return $row;
    }


    //查询一条语句的封装函数
    public static function getOne($field, $table, $where) {
        self::getInstance();
        $sql = "select $field from `$table` where $where";
        return self::getConn()->query($sql)->fetch(PDO::FETCH_ASSOC);

    }
    //查询一条记录
    public static function row($sql) {
        self::getInstance();
        return self::getConn()->query($sql)->fetch(PDO::FETCH_ASSOC);
    }
    //增加一条语句的封装函数
    //返回插入的ID
    public static function set($table, $data) {
        $str_key = '';
        $str_val = '';
        foreach ($data as $key => $val) {
            $str_key.='`' . $key . '`,';
            $str_val.="'" . $val . "',";
        }
        $str_key = substr($str_key, 0, -1);
        $str_val = substr($str_val, 0, -1);
        $sql = "insert into `$table` ($str_key) values($str_val)";
        self::getInstance();
        self::getConn()->exec($sql);
        return self::getConn()->lastInsertId();
    }

    //修改一条语句的封装函数
    //返回影响的行数
    public static function update($table, $where, $date) {
        $str_set = '';
        foreach ($date as $key => $val) {
            $str_set.="`" . $key . "`='" . $val . "',";
        }
        $str_set = substr($str_set, 0, -1);
        $sql = "update `$table` set $str_set where $where";
        self::getInstance();
        return self::getConn()->exec($sql);
    }

    //删除一条语句的封装函数
    //返回影响行数数
    public static function delete($table, $where) {
        $sql = "delete from `$table` where $where";
        self::getInstance();
        return self::getConn()->exec($sql);
    }
     
     //执行一条SQL语句
    public static function setquery($sql) {
        self::getInstance();
        $query = self::getConn()->query($sql); //执行上面的语句
        return $query;
    }
}

你可能感兴趣的:(PDO封装)