PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old May 7th, 2008, 05:17 AM
homerdooh homerdooh is offline
php newbie
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2001
Posts: 505 homerdooh User rank is Corporal (100 - 500 Reputation Level)homerdooh User rank is Corporal (100 - 500 Reputation Level)homerdooh User rank is Corporal (100 - 500 Reputation Level)homerdooh User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 3 h 11 m 5 sec
Reputation Power: 11
PHP-General - Undo MySQL Queries

Hi,

If you have many mysql update/insert/delete queries in a page how do you make sure they are all not executed if one of them fails.

E.g.

You have mysql queries..

insert
update
insert
insert
insert
insert
update

What if the 4th query fails for some reason, e.g. timeout, server crash, is there a way to make the first 3 queries "undo".

I know you can do mysql_affected_rows() and die() if the queries fail and the subsequest queries will not run but is there a way to affect the queries run before that?

Thanks.
__________________
"They have the internet on computers now" - Homer

Reply With Quote
  #2  
Old May 7th, 2008, 05:40 AM
SimonJM SimonJM is online now
Contributing User
Dev Shed Novice (500 - 999 posts) Click here for more information
 
Join Date: Mar 2006
Posts: 609 SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 23 h 38 m 37 sec
Reputation Power: 194
I think for that you'd need transactions, and that would require tables to be all innodb.
Comments on this post
ManiacDan agrees!
lnxgeek agrees!
__________________
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer" - Bruce Graham

Reply With Quote
  #3  
Old May 7th, 2008, 05:50 AM
PranK PranK is offline
Magical Liopleurodon
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2004
Location: Sydney, Australia
Posts: 569 PranK User rank is Second Lieutenant (5000 - 10000 Reputation Level)PranK User rank is Second Lieutenant (5000 - 10000 Reputation Level)PranK User rank is Second Lieutenant (5000 - 10000 Reputation Level)PranK User rank is Second Lieutenant (5000 - 10000 Reputation Level)PranK User rank is Second Lieutenant (5000 - 10000 Reputation Level)PranK User rank is Second Lieutenant (5000 - 10000 Reputation Level)PranK User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 h 2 m 47 sec
Reputation Power: 79
As simon said, look into transactions.

Basically, before all your queries are run, you start a transaction. What happens within the transaction can be looked at like a 'dry run' or a 'cache'. You can see the results, but they havent been properly applied. If there is an error, the transaction automatically rolls back, otherwise you can try to catch an error and rollback yourself. If all goes well, you can commit the transaction to make the changes final.

clicky
Comments on this post
lnxgeek agrees!
__________________
Regards,

Christian Biggins - Blogger Extraordinaire | Skylines Australia Owner | PHPOnIn Developer* | Good Question Asker Appreciater

* Work in Progress. Scheduled Version 1.0 release 01/04/2008

Reply With Quote
  #4  
Old May 7th, 2008, 10:05 AM
homerdooh homerdooh is offline
php newbie
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2001
Posts: 505 homerdooh User rank is Corporal (100 - 500 Reputation Level)homerdooh User rank is Corporal (100 - 500 Reputation Level)homerdooh User rank is Corporal (100 - 500 Reputation Level)homerdooh User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 3 h 11 m 5 sec
Reputation Power: 11
Thanks, much to read. Have any of you guys implemented this?

Reply With Quote
  #5  
Old May 7th, 2008, 10:16 AM
Veamon's Avatar
Veamon Veamon is offline
Blowing up trees with my mind.
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2005
Location: Neverland Ranch, minus the touching.
Posts: 2,604 Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 117515 Folding Title: Super Ultimate Folder - Level 1Folding Points: 117515 Folding Title: Super Ultimate Folder - Level 1Folding Points: 117515 Folding Title: Super Ultimate Folder - Level 1Folding Points: 117515 Folding Title: Super Ultimate Folder - Level 1Folding Points: 117515 Folding Title: Super Ultimate Folder - Level 1Folding Points: 117515 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Weeks 3 Days 7 h 41 m 41 sec
Reputation Power: 822
Quote:
Originally Posted by homerdooh
Thanks, much to read. Have any of you guys implemented this?


Yup. Works fine.
__________________

Raid1 in XP Pro

My open source projects:
------------------------
Blobber - Add images as blobs to SQL Server
------------------------

Reply With Quote
  #6  
Old May 7th, 2008, 04:51 PM
PranK PranK is offline
Magical Liopleurodon
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2004
Location: Sydney, Australia
Posts: 569 PranK User rank is Second Lieutenant (5000 - 10000 Reputation Level)PranK User rank is Second Lieutenant (5000 - 10000 Reputation Level)PranK User rank is Second Lieutenant (5000 - 10000 Reputation Level)PranK User rank is Second Lieutenant (5000 - 10000 Reputation Level)PranK User rank is Second Lieutenant (5000 - 10000 Reputation Level)PranK User rank is Second Lieutenant (5000 - 10000 Reputation Level)PranK User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 h 2 m 47 sec
Reputation Power: 79
Quote:
Originally Posted by homerdooh
Thanks, much to read. Have any of you guys implemented this?


Easy.

sql Code:
Original - sql Code
  1.  
  2. ALTER TABLE tablename ENGINE=InnoDB;
  3.  
  4. START TRANSACTION;
  5.  
  6. UPDATE tablename SET col='val' WHERE criteria='true';
  7.  
  8. SELECT * FROM tablename WHERE criteria='true';
  9.  
  10. -- Check your rows.
  11. -- If all good;
  12. COMMIT;
  13.  
  14. --If not so good;
  15. ROLLBACK;


I know a lot of the features in MySQL can be daunting. But just get your hands dirty, have a play. No better way to learn and its a lot easier than it seems.

Reply With Quote
  #7  
Old May 7th, 2008, 07:04 PM
Hammer65's Avatar
Hammer65 Hammer65 is offline
Web Developer/Musician
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2004
Location: Lincoln Nebraska
Posts: 1,927 Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hammer65 User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 19 h 8 m 50 sec
Reputation Power: 500
Send a message via AIM to Hammer65
There are several PHP based database abstractions that make this as easy as it is going to get.
__________________
"Strange women lying in ponds distributing swords is no basis for a system of government. Supreme executive power derives from a mandate from the masses, not from some farcical aquatic ceremony! Well, but you can't expect to wield supreme executive power just 'cause some watery tart threw a sword at you! I mean, if I went 'round saying I was an emperor just because some moistened bint had lobbed a scimitar at me, they'd put me away!"

Reply With Quote
  #8  
Old May 8th, 2008, 02:30 AM
SimonJM SimonJM is online now
Contributing User
Dev Shed Novice (500 - 999 posts) Click here for more information
 
Join Date: Mar 2006
Posts: 609 SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 23 h 38 m 37 sec
Reputation Power: 194
Quote:
Originally Posted by homerdooh
Thanks, much to read. Have any of you guys implemented this?

Yes, but only in IDMS/R on a mainframe

Reply With Quote
  #9  
Old May 8th, 2008, 09:23 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Beware
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2006
Location: Texas, USA
Posts: 1,435 ManiacDan User rank is First Lieutenant (10000 - 20000 Reputation Level)ManiacDan User rank is First Lieutenant (10000 - 20000 Reputation Level)ManiacDan User rank is First Lieutenant (10000 - 20000 Reputation Level)ManiacDan User rank is First Lieutenant (10000 - 20000 Reputation Level)ManiacDan User rank is First Lieutenant (10000 - 20000 Reputation Level)ManiacDan User rank is First Lieutenant (10000 - 20000 Reputation Level)ManiacDan User rank is First Lieutenant (10000 - 20000 Reputation Level)ManiacDan User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 16 h 53 m 15 sec
Reputation Power: 176
The ADODB database library for PHP supports transactions in MySQL, oracle, and a number of others. Transactions are as easy as:

PHP Code:
 $db->begin();
$db->query();
$db->query();
$db->query();
$db->query();
if ( 
$db->failed() ) {
   
$db->rollback();
} else {
   
$db->commit();

Or something like that. Been a while since I've used it.

-Dan
__________________
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - Undo MySQL Queries


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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