
将数据库连接放在类里面,用的时候直接实例化类。
将数据库地址,用户名,密码做成成员变量
将数据库连接做成成员方法,通过传入的sql语句返回结果集对象
class DBDA{
public $host="localhost";
public $uid="root";
public $pwd = "";
public $dbconnect;
function Query($sql,$type=1,$dbname="z-stu"){
$this->dbconnect = new MySQLi($this->host,$this->uid,$this->pwd,$dbname);
if(!mysqli_connect_error()){
$result = $this->dbconnect->query($sql);
if($type == 1){
$attr = $result->fetch_all();
return $attr;
}else{
return $result;
}
}else{
echo "连接失败";
}
}
}