MySQL-DB-封装-入门版

<?php

class MysqlDb{
    public $host = "127.0.0.1";
    public $user = "root";
    public $password = "root";
    public $dbname = "1903a";
    public $port = "3306";
    public $link;

    //构造方法
    function __construct()
    {
        $obj = mysqli_connect($this->host,$this->user,$this->password,$this->dbname,$this->port);
        if(!$obj){
            echo "连接数据库失败";
            die();
        }
        $this->link = $obj;
        mysqli_query($this->link,‘set names utf8‘);
        //
    }
    //增加
    function insert($sql){
        $res = mysqli_query($this->link,$sql);
        if($res){
            return true;
        }else{
            return false;
        }
    }
    //删除
    function delete($sql){
        $res = mysqli_query($this->link,$sql);
        if($res && mysqli_affected_rows($this->link)){
            return true;
        }else{
            return false;
        }
    }
    //修改
    function update($sql){
        $res = mysqli_query($this->link,$sql);
        return $res;
    }
    //查询
    function select($sql){
        $obj = mysqli_query($this->link,$sql);
        $data = mysqli_fetch_all($obj,1);
        return $data;
    }

    function findOne($sql){
        $obj = mysqli_query($this->link,$sql);
        return mysqli_fetch_assoc($obj);
    }

    function page($sql){
        $obj = mysqli_query($this->link,$sql);
        return mysqli_fetch_all($obj,1);
    }
    function __destruct()
    {
        // TODO: Implement __destruct() method.
        mysqli_close($this->link);
    }
}

 

MySQL-DB-封装-入门版

上一篇:MySQL Replication(数据同步技术)


下一篇:MySQL 导入导出数据