Php Mysqli交易无法正常工作

I have this code, trying to use transaction.

try 
{
  $mysqli->autocommit(FALSE); // Line (1)

  $queries = $query_delete_atividades . $query_insert_atividades . "SET NAMES 'utf8';" . $query_anuncio;

  if ($mysqli->multi_query($queries) === TRUE) {
      $resultado = '[{ "resultado" : "ok"}]';
      return $resultado;
  } else {
      return "";
  }

  $mysqli->autocommit(TRUE); // Line (2)
  mysqli_commit($mysqli); // Line (3)

  $query_error = mysqli_error($mysqli);

  if (strlen($query_error) == 0) 
  {
    $resultado = '[{ "resultado" : "ok"}]';
    return $resultado;
  }
} 
catch (Exception $e) 
{
  mysqli_rollback($mysqli); // Line (4)     
}

In the variable $queries I have three types of queries:

  • An Update
  • A Delete
  • Some Inserts

If I comment the lines (1), (2), (3) and (4) all the queries runs correctly (update, delete and inserts).

With those lines uncommented, nothing happens in the database.

My queries are correct and verified.

Is this code correct ? What am I doing wrong ?

Thanks.

Transaction will never work with the code you are using till you don't update this code return logic of before return commit

if ($mysqli->multi_query($queries) === TRUE) {
  $resultado = '[{ "resultado" : "ok"}]';
  return $resultado;
} else {
  return "";
}

As after this code execution you returning control from try block to previous function so to continue with this code with transaction you need to change commit position like this:

if ($mysqli->multi_query($queries) === TRUE) {
  $resultado = '[{ "resultado" : "ok"}]';

  mysqli_commit($mysqli); // Line (3)

  /* commit internally sets auto commit to true so $mysqli->autocommit(TRUE); // Line (2) not required */
  return $resultado;
} else {
  return "";
}