php-mysqli封装

/lib/Mysql.sql mysqli类文件

<?php


class Mysql
{
    //私有的属性
    private static $dbCon=false;
    private $host;
    private $port;
    private $user;
    private $pass;
    private $db;
    private $charset;
    private $link;
    public $errorNo;
    public $errMsg;

    //私有的构造方法
    private function  __construct($config=array()){
        $this->host = $config[‘host‘] ? $config[‘host‘] : ‘127.0.0.1‘;
        $this->port = $config[‘port‘] ? $config[‘port‘] : ‘3306‘;
        $this->user = $config[‘user‘] ? $config[‘user‘] : ‘root‘;
        $this->pass = $config[‘pass‘] ? $config[‘pass‘] : ‘root‘;
        $this->db = $config[‘db‘] ? $config[‘db‘] : ‘small2‘;
        $this->charset=isset($arr[‘$config‘]) ? $arr[‘$config‘] : ‘utf8sss‘;

        //连接数据库
        $this->connect();
        //选择数据库
        $this->useDb();
        //设置字符集
        $this->setCharset();
    }

    //连接数据库
    private function connect(){
        try {
            $this->link = @mysqli_connect($this->host . ‘:‘ . $this->port, $this->user, $this->pass);
            if (mysqli_connect_errno($this->link)) {
                $this->errorNo = mysqli_connect_errno($this->link);
                $this->errMsg = mysqli_connect_error();
                throw new Exception(‘数据库连接失败‘);
            }
        } catch (Exception $e) {
            echo $e->getMessage();
            exit;
        }
    }

    //设置字符集
    private function setCharset(){
        try {
            $bool = mysqli_query($this->link, "set names {$this->charset}");
            if (!$bool) {
                $this->errorNo = mysqli_errno($this->link);
                $this->errMsg = mysqli_error($this->link);
                throw new Exception(‘设置charset错误‘);
            }
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }

    //选择数据库
    private function useDb(){
        try {
            $bool = mysqli_query($this->link,"use {$this->db}");
            if (!$bool) {
                $this->errorNo = mysqli_errno($this->link);
                $this->errMsg = mysqli_error($this->link);
                throw new Exception(‘设置charset错误‘);
            }
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }

    //私有的克隆
    private function __clone(){
        die(‘clone is not allowed‘);
    }

    //单例模式
    public static function getInstance($config){
        if(self::$dbCon==false){
            self::$dbCon=new self($config);
        }
        return self::$dbCon;
    }

    //执行sql语句的方法
    public function query($sql){
        try{
            $res=mysqli_query($this->link,$sql);
            if(!$res){
                $this->errorNo = mysqli_errno($this->link);
                $this->errMsg = mysqli_error($this->link);
                throw new Exception(‘query()方法错误‘);
            }
            return $res;
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }

    //获得最后一条记录id
    public function getInsertId(){
        return mysqli_insert_id($this->link);
    }

    //查询表的字段
    public function getColumns($table){
        $sql = "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE table_name = ‘$table‘ AND table_schema = ‘$this->db‘";

        $query=$this->query($sql);
        $list=array();
        while ($r=$this->getFormSource($query)) {
            $list[]=$r[‘COLUMN_NAME‘];
        }

        return $list;
    }

    //获取一行记录,return array 一维数组
    public function getRow
上一篇:Mysql 占用字节大小


下一篇:sql太长的时候,在python执行会出现报错,可以用'''号来解决问题。