Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I use MySQL transactions in PHP?
    text
    copied!<p>I'm sorry, this is a very general question but I will try to narrow it down.</p> <p>I'm new to this whole transaction thing in MySQL/PHP but it seems pretty simple. I'm just using mysql not mysqli or PDO. I have a script that seems to be rolling back some queries but not others. This is uncharted territory for me so I have no idea what is going on.</p> <p>I start the transaction with <code>mysql_query('START TRANSACTION;')</code>, which I understand disables autocommit at the same time. Then I have a lot of complex code and whenever I do a query it is something like this <code>mysql_query($sql) or $error = "Oh noes!"</code>. Then periodically I have a function called <code>error_check()</code> which checks if <code>$error</code> is not empty and if it isn't I do <code>mysql_query('ROLLBACK;')</code> and <code>die($error)</code>. Later on in the code I have <code>mysql_query('COMMIT;')</code>. But if I do two queries and then purposely throw an error, I mean just set $error = something, it looks like the first query rolls back but the second one doesn't.</p> <p>What could be going wrong? Are there some gotchas with transactions I don't know about? I don't have a good understanding of how these transactions start and stop especially when you mix PHP into it...</p> <p><strong>EDIT:</strong> My example was overly simplified I actually have at least two transactions doing INSERT, UPDATE or DELETE on separate tables. But before I execute each of those statements I backup the rows in corresponding "history" tables to allow undoing. It looks like the manipulation of the main tables gets rolled back but entries in the history tables remain.</p> <p><strong>EDIT2:</strong> Doh! As I finished typing the previous edit it dawned on me...there must be something wrong with those particular tables...for some reason they were all set as MyISAM.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload