langley Posted August 3, 2006 Share Posted August 3, 2006 Hi, I have to insert multiple lines of user details into 2 different tables.I currently have a script that loops through the lines and adds accordingly.However if there is an error I need to undo all the entries I have made.Could somebody tell me will transaction rollback help me in this case?I currently have the following which is not working where the transaction code is out side the loop.I'm assuming this is wrong but I'm trying to find a way to avoid having to step back through 50 entries for example if the error is produced in the last line code follows - thanks for any help or info....$how_many_errors=0;mysql_query('BEGIN TRANSACTION_NAME');for($b=0;$b<$blah;$b++){//do some SQL queries if there is an error increment $how_many_errors}if($how_many_errors>0) { mysql_query('ROLLBACK');}else {mysql_query('COMMIT');} Quote Link to comment Share on other sites More sharing options...
CTM Posted August 3, 2006 Share Posted August 3, 2006 You should treat the error right way instead of doing a rollback. Quote Link to comment Share on other sites More sharing options...
shocker-z Posted August 3, 2006 Share Posted August 3, 2006 From what i've read this should work for you.. her's some info i found from searching around."Do not use transactions (e.g. with InnoDB MySQL tables) with persistent connections. If your script stops or exits for any reason, your transaction will be left open and your locks will be left on. You have to reset MySQL to release them (so far as I can figure). They won't ROLLBACK automatically on error, like they ought to. When you restart the script, you'll get a new connection, so you can't rollback or commit for the previous script. Any script with a start transaction, rollback, or commit SQL statement should use regular (not persistent) connections. Seems like PHP ought to automatically issue a ROLLBACK on any open transactions when a script exits (error or otherwise) without a COMMIT. ZEND's site has a brief blurb on this. It's OK to mix/match so you use a persistent connection for the read stuff, but open a new regular connection conditionally (if you have to update, which is usually less often)."Andy also this link if you havn't seen it yet [url=http://dev.mysql.com/doc/refman/5.0/en/commit.html]http://dev.mysql.com/doc/refman/5.0/en/commit.html[/url]RegardsLiam Quote Link to comment Share on other sites More sharing options...
langley Posted August 3, 2006 Author Share Posted August 3, 2006 [b]CTM[/b] - I realise that but if for whatever reason there is some kind of failiure to complete inserting of ALL users I need to take out any that may have been inserted by the loop thusfar and I'm trying to avoid going back and deleting each set of details from both tables 1 by 1[b]shocker-z[/b] Thanks for the info - I had a quick read and tried setting autocommit to 0 but it's still not rolling back. I'm finding it pretty confusing to figure out exactly what you can and can't do with transactions. Where you quoted "Do not use transactions (e.g. with InnoDB MySQL tables) with persistent connections." does that mean I have to reconnect for each query? if so it kind of defeats the object of me trying to use rollback. Sorry if these are dumb questions, I'm no master of this stuff! Quote Link to comment Share on other sites More sharing options...
shoz Posted August 3, 2006 Share Posted August 3, 2006 The persistent connection comment has to do with the fact that PHP will reuse the connection. Meaning any open transaction will still be open for the next script that uses the connection. The short answer for that is to use [url=http://www.php.net/mysql_connect]mysql_connect()[/url] instead of [url=http://www.php.net/mysql_pconnect]mysql_pconnect()[/url].As far as your question is concerned. If you're saying you simply want to undo all that you've done if everything didn't go according to plan, then rolling back the transaction will be fine. You should make your table types "INNODB" if you'd like to do that.http://www.innodb.com/ibman.phphttp://dev.mysql.com/doc/refman/5.0/en/innodb.html Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 3, 2006 Share Posted August 3, 2006 shoz wouldnt it work if the user set a session with the old entries then use a simple update page Quote Link to comment Share on other sites More sharing options...
shoz Posted August 3, 2006 Share Posted August 3, 2006 [quote author=redarrow link=topic=102905.msg409187#msg409187 date=1154639197]shoz wouldnt it work if the user set a session with the old entries then use a simple update page[/quote]I don't fully understand what you mean but the main thing to remember is that depending on the degree to which you change the database it becomes very difficult to manually keep track yourself.In addition, depending on the error that occurs while making the changes it can become even more complicated. If it's critical that multiple entries/changes occur to the database without error, use the INNODB engine and Transactions. Quote Link to comment Share on other sites More sharing options...
langley Posted August 4, 2006 Author Share Posted August 4, 2006 thanks to everyone for their help - as a result it is now working ;D Quote Link to comment 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.