I have three sql queries in a try/catch block. Autocommit is off and all three queries run without errors, I have verified that (it shouldn't reach the commit line if any query fails, but I've verified it via xdebug, stepping through the code one line at a time). When the code reached the commit() function call, mysqli returns false, but no error message. How can a commit fail when the previous queries all worked?
This is basically how the code looks:
try{
$mysqli->autocommit(false);
$sql1 = "insert into...";
$mysqli->query($sql); // Works!
if($mysqli->error)
{
$mysqli->rollback();
throw new Exception....
}
$sql2 = "insert into...";
$mysqli->query($sql); // Works!
if($mysqli->error)
{
$mysqli->rollback();
throw new Exception....
}
$mysqli->commit(); // Fail
}
Check to have: $mysqli->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
(of course if you want to write/insert in the table)
Try to debug following these steps: [ -- start transaction code --]
$q2=$mysqli->query($query2);// query inside transaction
print 'q2:'.$mysqli->error;
[ -- commit /rollback condition / code --------]
You may receive this error: "Cannot execute statement in a READ ONLY transaction." -> change the parameter of begin transaction to MYSQLI_TRANS_START_READ_WRITE
Also check to have the innoDB engine set for the tables on which you want to use transactions.
You may also test the queries outside the transaction block, to make sure they are error-free.