Tazz Posted March 16, 2011 Share Posted March 16, 2011 I am new to MySQLI and tried to test the rollback functionality. I created a simple database called mysqli and one table called test with and id and name field. my PHP is as follows: $mysqli = new mysqli('localhost',name,password,'mysqli'); $mysqli->autocommit(FALSE); $result = $mysqli->query("INSERT INTO test (name) VALUES('bla')"); $mysqli->commit(); $mysqli->rollback(); After hitting the page, it does the insert, but does not roll back the transaction. So it ends up being inserted into the DB. Why is the rollback not working? Quote Link to comment https://forums.phpfreaks.com/topic/230780-mysqli-rollback-not-working/ Share on other sites More sharing options...
bh Posted March 16, 2011 Share Posted March 16, 2011 Hi, you've done a commit, so your commands executes and uploaded into your db. If you want to rollback your exec doesnt call commit before rollback. $mysqli->autocommit(FALSE); // these command will be rollbacked $mysqli->rollback(); use exception to handle an elegant way to commit/rollback Quote Link to comment https://forums.phpfreaks.com/topic/230780-mysqli-rollback-not-working/#findComment-1188073 Share on other sites More sharing options...
Tazz Posted March 16, 2011 Author Share Posted March 16, 2011 Thanks for the reply, so I cannot rollback anything already committed? That seems a bit weird as all the examples I've seen online suggests that. Quote Link to comment https://forums.phpfreaks.com/topic/230780-mysqli-rollback-not-working/#findComment-1188075 Share on other sites More sharing options...
bh Posted March 16, 2011 Share Posted March 16, 2011 If you start a transaction, that means you have an active transaction. You can finish your active transaction with a commit or a rollback. After you commited a transaction you cant rollback it. So start a transaction, check error, if theres an error rollback your transaction, else commit it. Some useable info: You cannot create a transaction on non-transaction tables (eg MyIsam), and you cannot rollback DDL statements. Quote Link to comment https://forums.phpfreaks.com/topic/230780-mysqli-rollback-not-working/#findComment-1188078 Share on other sites More sharing options...
Tazz Posted March 16, 2011 Author Share Posted March 16, 2011 Ahh... That makes sense! Thanks for telling me about the fact that I need to make sure my tables are InnoDB, all mine were MyISAM. I'll play around with it a little bit more, but the way you described it to me now makes perfect sense. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/230780-mysqli-rollback-not-working/#findComment-1188114 Share on other sites More sharing options...
bh Posted March 16, 2011 Share Posted March 16, 2011 Your welcome. Quote Link to comment https://forums.phpfreaks.com/topic/230780-mysqli-rollback-not-working/#findComment-1188115 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.