博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

MySqli 执行多条SQL语句

Posted on 2014-07-15 00:05  SmarTom  阅读(1731)  评论(0编辑  收藏  举报

使用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());    }