您的位置:首页 > 数据库 > MySQL

MySqli 执行多条SQL语句

2014-07-15 00:05 274 查看
使用multi_query(); 去执行SQL语句,执行多条语句多个SQL语句用“;”分开

一:没有结果集的语句:

$sql="insert into products (cid,name,price,num) values('2','PHP','2.22','10');update products set cid='10',name='tong',price='33.33',num='20' where id > 10; delete from products where id< 4";
$mysqli->multi_query($sql);


二:有结果集的语句: 

$sqls.="select current_user();";
$sqls.="desc products;";
$sqls.="select * from products;";
$sqls.="select current_date()";
echo "------执行有结果集的-----------------------><br>";
if($mysqli->multi_query($sqls)){
do{
$result=$mysqli->store_result();        //获取结果集
echo "<table border='1' width='600'>";
echo "<tr>";
while($filds=$result->fetch_field()){
echo "<th>{$filds->name}</th>";
}
echo "</tr>";
while($row=$result->fetch_row()){
echo "<tr>";
foreach($row as $col){
echo "<td>{$col}</td>";
}
echo "</tr>";
}
echo "</table>";
if($mysqli->more_results()){
echo "<br></br>";
}
}while($mysqli->next_result());    }


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: