How to perform a mysql rollback in php?

Reply

Join Date: Aug 2004
Posts: 24
Reputation: ray_broome is an unknown quantity at this point 
Solved Threads: 0
ray_broome ray_broome is offline Offline
Newbie Poster

How to perform a mysql rollback in php?

 
0
  #1
Oct 21st, 2008
Hi,

I have a situation where i am inserting to two tables from a web form but the first table needs to be inserted to so i can get the PK, then use the PK to insert into the second table. So what I would like to do is rollback the insert operation if there is some error inserting to the first table or vice versa.

This is what i'm thinkin of doin so far:
  1. mysql_query("START TRANSACTION");
  2. if(mysql_query($myquery))
  3. mysql_query("COMMIT");
  4. else
  5. mysql_query("ROLLBACK");

Any help/ideas/suggestions?

Thanks in advance...
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: How to perform a mysql rollback in php?

 
0
  #2
Oct 21st, 2008
so pretty much you want to take the insertion uid and use it in the second table.

something like this maybe.

  1. $sql = "SQL HERE";
  2. $query = mysql_query( $sql,$con );
  3. if ( $query ) {
  4. $iid = mysql_insert_id( $con );
  5. $sql = "SQL HERE WITH {$iid}";
  6. $query = mysql_query( $sql );
  7. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: How to perform a mysql rollback in php?

 
0
  #3
Oct 22nd, 2008
This is how I usually do it:
  1. <?php
  2. $querylog = "TRANSACTION STARTED<br /><br />";
  3. $commit = "commit";
  4. mysql_query("begin", $dbconn);
  5.  
  6. $query = "insert into ...";
  7. if(!mysql_query($query, $dbconn))
  8. {
  9. $commit = "rollback";
  10. $querylog .= "error in query: " . $query . " : " . mysql_error($dbconn) . "<br /><br />";
  11. }
  12.  
  13. $query = "insert into ...";
  14. if(!mysql_query($query, $dbconn))
  15. {
  16. $commit = "rollback";
  17. $querylog .= "error in query: " . $query . " : " . mysql_error($dbconn) . "<br /><br />";
  18. }
  19.  
  20. $query = "insert into ...";
  21. if(!mysql_query($query, $dbconn))
  22. {
  23. $commit = "rollback";
  24. $querylog .= "error in query: " . $query . " : " . mysql_error($dbconn) . "<br /><br />";
  25. }
  26.  
  27. if($commit == "rollback")
  28. {
  29. $querylog .= "ERROR IN TRANSACTION<br /><br />transaction rolled back<br /><br />";
  30. //echo $querylog;
  31. }
  32.  
  33. mysql_query($commit);
  34. ?>
Last edited by R0bb0b; Oct 22nd, 2008 at 3:46 am.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 24
Reputation: ray_broome is an unknown quantity at this point 
Solved Threads: 0
ray_broome ray_broome is offline Offline
Newbie Poster

Re: How to perform a mysql rollback in php?

 
0
  #4
Oct 22nd, 2008
cool, thanks for the help guys, r0bb0b's solution seems closer to what i'm trying to do though because it is supposed to be an "all or nothing" situation
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC