Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/230780-mysqli-rollback-not-working/
Share on other sites

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

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.

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.