【php高级】MySQLI类

本文介绍了一个用于操作 MySQL 数据库的 PHP 类,包括连接数据库、执行 SQL 语句等功能,并支持批量执行 SQL 语句。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

db=$db;//默认为二大系统数据库之一,保证SQL语句都能执行
$this->host=HOST;
$this->user=USER;
$this->pwd=PWD;
}

//封装需要操作的数据属性
function setDb($value){
$this->db=$value;
}

function getDb(){
return $this->db;
}

/*
Describe:切换数据库服务器
Paramters:
$host 数据库服务器的地址
$user 数据库服务器的帐号
$pwd 数据库服务器的密码
*/
function ChangeDBHost($host,$user,$pwd){
$this->host=$host;
$this->user=$user;
$this->pwd=$pwd;
}


/*
Describe:操作MYSQL数据库
Paramters:$sql任何的SQL语句,$type=1默认引用数组,$type=2索引数组
Returns:读取返回结果二维数组,添加/修改/删除返回影响行数
*/
function Execute($sql,$type=1){
$mysqli=@new mysqli($this->host,$this->user,$this->pwd);
if($mysqli->connect_error)
die('连接数据库服务器失败,请检查配置');
@$mysqli->select_db($this->db) or die('访问的数据库'.$this->db.'不存在,请重新输入');
$mysqli->query("set names utf8");
$result=@$mysqli->query($sql);
if(substr_count($sql,"select ")==0)
{
$mysqli->close();//关闭链接释放资源
return $result;
}
//$arr='';//读取返回的二维数组
if($type==1){
while(@$row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
	$arr[]=$row;
	}
}
elseif($type==2){
while(@$row=mysqli_fetch_array($result,MYSQLI_NUM)){
	$arr[]=$row;
	}
}
$mysqli->close();
return $arr;
}

//批量执行SQL语句
function ExecuteMultiSql($sql){
$mysqli=@new mysqli($this->host,$this->user,$this->pwd);
if($mysqli->connect_error)
die('连接失败,请检查配置');
@$mysqli->select_db($this->db) or die('访问的数据库'.$this->db.'不存在');
$mysqli->multi_query($sql);
$mysqli->close();
}

/*
批量执行SQL语句 $sqls批量执行SQL语句,中间以;隔开
最后一条SQL查询返回最后一个结果集
最后一条SQL插入、修改、删除返回影响行数
*/
function ExecuteArr($sqls){
//1、创建mysqli对象
$mysqli=@new mysqli($this->host,$this->user,$this->pwd,$this->db);
if($mysqli->connect_error)
die("连接失败".$mysqli->connect_error);
//2、处理结果
//如果成功,至少有一个结果集
if($res=$mysqli->multi_query($sqls)){
	while($mysqli->next_result()){
		$res=$mysqli->store_result();
		//必须强制跳出循环
		if(!$mysqli->more_results())
		break;
	}
}
//3、关闭资源
$mysqli->close();
return mysqli_fetch_array($res);
}

}
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值