How to do multiple queries to SQL

前端 未结 2 557
醉话见心
醉话见心 2021-01-29 06:06

I am trying to create a table and , insert some values in database with help of PHP script. While inserting only 1 row , it works fine. An when I try to enter more number of row

相关标签:
2条回答
  • 2021-01-29 06:51

    Use mysqli_multi_query instead of mysqli_query, that should do it.

    $query = mysqli_multi_query($db_conx,$sqlCommand) or die(mysqli_error($db_conx));
    

    http://php.net/manual/en/mysqli.multi-query.php

    0 讨论(0)
  • 2021-01-29 07:02

    An easier approach could be to split the string with ; and iterate of each query in the array. Execute all of them one by one..

    $sqlCommandArray[]=explode(";",$sqlCommand);
    
    for($i=0;$i<count($sqlCommandArray);$i++){
    try{
      mysqli_query($db_conx,$sqlCommandArray[$i]);
    } 
    catch(Exception $ex){
      echo "there was an error in executing the queries";
    }
    }
    

    This would do the job without any modifications.

     $query = mysqli_multi_query($db_conx,$sqlCommand);
    
    0 讨论(0)
提交回复
热议问题