【二十四】使用mysqli扩展类批量执行多条sql语句

批量处理多个sql语句

<?php
//批量执行多个dml语句
    // $mysqli=new mysqli("localhost","root","","test");
    // if ($mysqli->connect_error) {
    //     die($mysqli->connect_error);
    // }
    // $sql="insert into user1(name,Password,Email,age) values('vv','vv','vv@qq.com',12);";
    // $sql.="insert into user1(name,Password,Email,age) values('v','v','v@qq.com',12)";
    // $sql.="insert into user1(name,Password,Email,age) values('qwe','qwe','qwe@qq.com',12)";
    // $a=$mysqli->multi_query($sql);
    // if (!$a) {
    //     echo "执行失败".$mysqli->error;
    // }else{
    //     echo "ok";
    // }
    // $mysqli->close();
//批量操作dql语句
    $mysqli=new mysqli("localhost","root","","test");
    if ($mysqli->connect_error) {
        die($mysqli->connect_error);
    }
    $sql="select * from user1;";
    $sql.="select * from user1 where name='vv';";
    $sql.="desc user1;";
    //如果成功代表有结果集
    //执行sql
    if ($res=$mysqli->multi_query($sql)) {
        // 循环获取结果集
        do{
            if ($result=$mysqli->store_result()) {
                //获取第一个结果集
                while ($row=$result->fetch_row()) {
                    foreach ($row as $key => $value) {
                        echo "----$value";
                    }
                    echo "<br/>";
                }
                //释放结果集
                $result->free();
            }
            //判断是否有更多结果集
            if (!$mysqli->more_results()) {
                break;
            }
            echo "*************新的结果集**************<br/>";
        }while($mysqli->next_result());
        //循环下一个结果集的内容
    }
?>

打印结果:

posted @ 2017-12-14 08:58  花花妹子。  阅读(445)  评论(0编辑  收藏  举报