I have the following code:
...
$mysqli->autocommit(FALSE);
if(in_array('files',$item)){
$sql_el_truncate = "
TRUNCATE TABLE `article_files`;
TRUNCATE TABLE `files`;
TRUNCATE TABLE `gallery_files`;
TRUNCATE TABLE `image_captions`;
";
$mysqli->multi_query($sql_el_truncate);
do{
$mysqli->use_result();
}while ($mysqli->next_result());
if ($mysqli->errno) {
$valid_entry = 0;
}
echo $valid_entry;
}
if( $valid_entry){
$mysqli->commit();
}else{
$mysqli->rollback();
}
...
On error I get a proper $valid_entry value of 0, but the rollback is not working. Even if I write on purpose some of the TRUNCATE commands falsely it will still run the rest commands, give me the error but it won’t rollback.
Anyone knows how can I properly use the transaction on the mysqli?
From the MySQL Reference Manual for
TRUNCATE TABLE(emphasis is mine):If you want to be able to
ROLLBACKyou are going to have to useDELETEstatements instead. They are less efficient here, but should give you the desired behavior.