Question

I am trying to execute a bunch of statements inserting into multiple tables within a transaction, but when I call for a rollback, everything returns without error but the data is inserted!

//Returns "YES"
$supports = mysql_query( "SELECT SUPPORT FROM INFORMATION_SCHEMA.ENGINES WHERE ENGINE = 'InnoDB'" );

//Start transaction
mysql_query( "SET AUTOCOMMIT=0" );
mysql_query( "START TRANSACTION" );

//Do some inserts
mysql_query( "INSERT INTO ...." );
$lastId = mysql_query( "SELECT LAST_INSERT_ID() as lastId" );
mysql_query( "INSERT INTO...." );

//Try to rollback
mysql_query( "ROLLBACK" );
mysql_query( "SET AUTOCOMMIT=1" );

The tables are all created as InnoDB tables. Running SHOW CREATE TABLE <table_name> shows all tables are created as:

CREATE TABLE `mytable` (
    ...the columns...
) ENGINE=InnoDB AUTO_INCREMENT=61 DEFAULT CHARSET=latin1

What am I doing wrong?

Was it helpful?

Solution

@eggyal led to the answer. My START TRANSACTION was inside a loop instead of outside it, so it was starting multiple transactions and only rolling back the last one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top