How can I do multiple queries, the usen commit(), and if something goes wrong, rollback all queries?
I noticed that if I wrap my queries and commit with inside try / catch, only the unsuccesful queries are rolled back
try{
$pdo->beginTransaction();
// create 10 tables
foreach($queries as $query)
$result = $pdo->query($query);
$pdo->commit();
}catch(PDOException $e){
// here if one of the tables fail to be created, undo all operations
$pdo->rollBack();
}
MySQL does not support embedded transactions.
If you think you are getting a partial rollback, you are checking it wrong. In particular, an unsuccesful query will not even run, so there’s nothing to rollback there. What happens is that you don’t really have a transaction, either because you forgot the
START TRANSACTIONstatement or because your tables are not InnoDB.Edit: I’ve just seen you’ve updated your question. You cannot rollback DDL statements such as
CREATE TABLE. You can only rollback DML statements (SELECT,INSERT,UPDATE…). This is not a MySQL limitation, it’s the standard behaviour in all DBMS engines I know.