mysqli的增强功能

批量执行sql语句


批量执行dml语句
基本语法
$sqls="sql1.sql2.sql3...."
mysqli::multi_query($sqls)
案例:
$mysqli=new mysqli("localhost","root","","test");
if($mysqli->connect_error){
die($mysqli->connect_error);
}
$sqls="insert into user1(name,password,email,age)
values('宋江','aaa','aa@qq.com',45);";
$sqls.="insert into user1(name,password,email,age)
values('吴用','aaa','aa@qq.com',46);";
$sqls.="insert into user1(name,password,email,age)
values('李逵','aaa','aa@qq.com',47);";

$a=$mysqli->multi_query($sqls);
if(!$a){
echo "执行失败";
}else{
echo "成功";
}
$mysqli->close();

批量执行DML语句可以混合使用delete insert update 语句,最好不要使用select语句。


批量执行dql语句
它可以一次性的取回多个结果集

//打开mysqli对象
$mysqli=new mysqli("localhost","root","","test");
//批量查询
$sqls="select * from emp;";
$sqls.="select*from user1;";
//处理结果
if($res=$mysqli->multi_query($sqls)){
do{
//从mysqli连接取出第一个结果集
$result=$mysqli->store_result();
//显示mysqli result对象
while($row=$result->fetch_row()){
foreach($row as $key => $val){
echo"--$val";
}
echo"

";
}
$result->free();
echo"

新的结果集

";
}while($mysqli->next_result());
}


上一篇:虚机centos和本机Windows之间文件的拷贝无法用xftp时用FileZilla也行


下一篇:bzoj 3295: [Cqoi2011]动态逆序对(树套树 or CDQ分治)