简体   繁体   中英

Checking INSERT in mysqli_multi_query() Procedural

I have a project file, and i have trouble at my syntax, example:

mysqli_query($connection,"START TRANSACTION");
$mysql_query = mysqli_multi_query($connection,"
    INSERT INTO `TABLE 1` (`ID`, `VAL`) VALUES (NULL, 'NAMBAH 1');
    INSERT INTO `TABLE 2` (`ID`, `VAL`) VALUES (NULL, 'NAMBAH 2');
    INSERT INTO `TABLE 3` (`ID`, `VAL`) VALUES (NULL, 'NAMBAH 3');
");
if(!$mysql_query){
    echo "Syntax MySQL: " . mysqli_error($connection);
}else{
    do{
    // Store first result set

    if ($result=mysqli_store_result($connection)){
        mysqli_free_result($result);
    }
  }while (mysqli_next_result($connection));
}

if imposible, i want to rollback if one MySQL Query is failed to inserted, i think we can check one by one for this query,

thanks for advance!.

It should be something like this:

mysqli_autocommit($connection, FALSE);
$mysql_query = mysqli_multi_query($connection,"
    INSERT INTO `TABLE 1` (`ID`, `VAL`) VALUES (NULL, 'NAMBAH 1');
    INSERT INTO `TABLE 2` (`ID`, `VAL`) VALUES (NULL, 'NAMBAH 2');
    INSERT INTO `TABLE 3` (`ID`, `VAL`) VALUES (NULL, 'NAMBAH 3');
");
  do{
    // Store first result set

    if ($result=mysqli_store_result($connection)){
        mysqli_free_result($result);
    }
  }while (mysqli_next_result($connection));
  if( $mysqli_errno($connection)){
      $mysqli_commit($connection);
  }else{
      echo "MySQL Error: " . mysqli_error($connection);
      $mysqli_rollback($connection);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM