问题
The rollback of my transaction doesn't work. How do I set autocommit to false (or 0) in the php script using PDO (I have InnoDB 5.7.18) ?
Here is my code:
global $bdd; //defined with PDO
try {
$bdd->beginTransaction();
/* my requests */
$bdd->commit();
} catch (Exception $e) {
$bdd->rollBack();
return $e->getMessage();
}
return true;
}
回答1:
$db = new PDO('mysql:dbname=employee');
$db->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
var_dump($db->query('SELECT @@autocommit')->fetchAll());
回答2:
I solved my problem myself: a few of my tables were in MyISAM (whereas the majority are in InnoDB -> I work with an old database system...); so the rollback didn't work for these tables. Once I changed them into InnoDB, it worked. Thanks to everybody for the help !
回答3:
Try setting the following attribute:
$bdd->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
来源:https://stackoverflow.com/questions/45234859/autocommit-with-pdo