数据库包装成类

 <?php

class DBDA
{
    public $host = "localhost"; //服务器地址
    public $uid = "root"; //数据库的用户名
    public $pwd = "123"; //数据库的密码
    
    //执行SQL语句,返回相应结果的函数
    //$sql是要执行的SQL语句
    //$type是SQL语句的类型,0代表增删改,1代表查询
    //$db代表要操作的数据库
    public function Query($sql,$type=1,$db="mydb")
    {
        //造连接对象
        $conn = new MySQLi($this->host,$this->uid,$this->pwd,$db);
        
        //判断连接是否成功
        !mysqli_connect_error() or die("连接失败!");
        
        //执行SQL语句
        $result = $conn->query($sql);
        
        //判断SQL语句类型
        if($type==1)
        {
            //如果是查询语句返回结果集的二维数组
            return $result->fetch_all();
        }
        else
        {
            //如果是其他语句,返回true或false
            return $result;
        }
    }
    
}
原文地址:https://www.cnblogs.com/hellodp/p/5339466.html