mysqli扩展库增强①批量执行SQL语句

1.批量执行dml语句
案例演示:sqls1.php
******************************************
sqls1.php


//一次性添加三个用户到用户表
    $mysqli=new mysqli("localhost","root","root1142495240","123");
    if ($mysqli->connect_error){
        die($mysqli->connect_error);
    }
    $sqls="insert into user1 (name,password,age) values('周杰困','1111','45');";
    $sqls.="insert into user1 (name,password,age) values('周杰困','1111','45');";
    $sqls.="insert into user1 (name,password,age) values('周杰困','1111','45');";
    $b=$mysqli->multi_query($sqls);
    if (!b){
        echo "添加失败";
    }else {
        echo "ok";
    }
    $mysqli->close();

2.批量执行dql语句
批量查询语句
演示案例:sqls2.php
*************************************/
sqls2.php


    $mysqli=new mysqli("localhost","root","root1142495240","123");
    $sqls="select * from user1;";
    $sqls.="select * from words;";
    if ($res=$mysqli->multi_query($sqls)){
        do {
            //取出第一个结果集
            $result=$mysqli->store_result();
            //显示mysqli_result对象
            while ($row=$result->fetch_row()){
                foreach ($row as $key=>$val){
                    echo "--$val";
                }
                echo "
"
; } //释放第一个结果集的资源 $result->free(); if (!$mysqli->more_results()){ break; } echo "**********另一个******
"
; }while ($mysqli->next_result()); } $mysqli->close();

你可能感兴趣的:(php)