1

I'm facing a strange issue with a transaction, this is the code:

$mysqli = $this->getMysqli();
    //Avoid Autocommitting
    $mysqli->autocommit(FALSE);       
    $mysqli->begin_transaction();

    $insertStatment = $mysqli->prepare('INSERT INTO '.$this->table.' (name, applied_at) VALUES (?, NOW())');
    try
    {    
        $mysqli->multi_query($sql);
        $insertStatment->bind_param('s', $name);
        $insertStatment->execute();
        $insertStatment->close();
        $mysqli->commit();
        $mysqli->close();
        return TRUE;
    }
    catch (Exception $e)
    {
      $mysqli->rollBack();
      return $e->getMessage(); 
    }

The $insertStatement gets completely ignored by the commit(). If I comment this line:

$mysqli->multi_query($sql);

It works perfectly, is there any issue when mixing prepared statements and multi_query? I need multi query because the $sql variable contains multiple ; separated sql statements. Is there any way to prevent this issue?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
0plus1
  • 4,475
  • 12
  • 47
  • 89

0 Answers0