SunQuest
 
           MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old February 11th, 2004, 10:15 PM
absolut absolut is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 1 absolut User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
mySQL Transaction Rollback

I'm new to PHP & mySQL, and currently I'm working on a function which does the following: connect to db, insert into 3 different tables, close the connection. Here's the code:

$db = mysql_connect("localhost", "root", "pwd") or die("Could not connect: ".mysql_error());
mysql_select_db("HR", $db);
$query = "INSERT INTO tblA (Field1, Field2) values ('a', 'b');
$sql = mysql_query($query) or die("Query Error: ".mysql_error());
$IndexNo = mysql_insert_id();

$query2 = "INSERT INTO tblB (Index, Field) values ($IndexNo, 'b');
$sql = mysql_query($query2) or die("Query Error: ".mysql_error());

$query3 = "INSERT INTO tblC (Index, Field) values ($IndexNo, 'b');
$sql = mysql_query($query3) or die("Query Error: ".mysql_error());

mysql_close($db);

Now, I don't want it to autocommit. So in case there's error while inserting tblC, the insert statement for tblA and tblB will be cancelled. I know I have to use start transaction and rollback if there's an error. But I'm not sure about the coding. Can anyone help?

Thanks a lot!

Reply With Quote
  #2  
Old February 12th, 2004, 12:26 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
MySQL is set to autocommit by default...

Try using SET AUTOCOMMIT=0 to disable autocommiting

Also realize, you must use a recent MySQL version which supports InnoDB tables

Notice that when doing that you need to *always* use commit to save changes...

To change it so you only autocommit in a transaction, try this:

Code:
START TRANSACTION;
UPDATE table SET summmary='whatever' WHERE type=1;
COMMIT;


PHP *does* support transactions, use seperate query statements for each command...

PHP Code:
 mysql_query("BEGIN");
mysql_query("COMMIT");
mysql_query("ROLLBACK"); 


hopefully this helps a bit?

MySQL documentation:
http://www.mysql.com/doc/en/COMMIT.html

MySQL's growing up: Transactions [web monkey]:
http://hotwired.lycos.com/webmonkey...dex3a_meta.html

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > mySQL Transaction Rollback


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway