在数据库中查询数据——方法封装
<?php
class DBDA
{
public $host="localhost"; //数据库地址
public $uid = "root"; //数据库用户名
public $pwd = ""; //密码
//执行sql语句,返回相应的结果
//参数:$sql代表执行的sql语句;$type是sql语句类型0代表查询,1代表其他;$db代表操作的数据库
public function Query($sql,$type=0,$db="mydb")
{
//1.连接数据库
$dbconnect=new MySQLi($this->host,$this->uid,$this->pwd,$db);
//2.判断是否出错
!mysqli_connect_error() or die("连接失败!");
//3.执行sql语句
$result=$dbconnect->query($sql);
if($type==0)
{
return $result->fetch_all();
}
else
{
return $result;
}
}
}
在以后的数据库查询中可以直接调用

浙公网安备 33010602011771号