PHP mysqli強化一括実行sql文の実装コード

2486 ワード

mysqli強化-sql文を一括実行
 
  

    //mysqli - sql

    // dql
    // mysqli mysqli::multi_query() 3

    $mysqli =new MySQLi("localhost","root","root","test");
    if($mysqli->connect_error){
        die (" ".$mysqli->connect_error);
    }

    //
    $sqls="insert into user1 (name,password,email,age) values('AAA',md5('AAA'),'[email protected]',25);";
    $sqls.="insert into user1 (name,password,email,age) values('BBB',md5('BBB'),'[email protected]',25);";
    $sqls.="insert into user1 (name,password,email,age) values('CCC',md5('CCC'),'[email protected]',25);";

    // dml delete insert update select
    //$sqls.="update user1 set age=15 where id=1;";
    //$sqls.="delete from user1 where id=10";

    $res=$mysqli->multi_query($sqls);

    if(!$res){
        echo " ".$mysqli->error;
    }else{
        echo "OK";
    }

?>


2、一括照会
 
  

    // mysqli mysqli::multi_query()

    //1、 mysqli
    $mysqli=new MySQLi("localhost","root","root","test");
    if($mysqli->connect_error){
        die(" ".$mysqli->connect_error);
    }
    //2、
    $sqls="select *from user1;";
    $sqls.="desc user1";
    //3、
    // ,
    if($res=$mysqli->multi_query($sqls)){

        do{
            //
            $res=$mysqli->store_result();
            while($row=$res->fetch_row()){
                foreach($row as $val){
                    echo '--'.$val;
                }
                echo '
';
            }

            //
            $res->free();
            //
            if($mysqli->more_results()){
                echo "******** ***************
";
            }else{
                break;
            }

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

    }   
    //4、
    $mysqli->close();
?>