TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-29-2009, 06:54 PM   #1 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default mysql ROLLBACK & COMMIT

I'm looking for some guidance on how to use mysql BEGIN, ROLLBACK and COMMITS. I have situations where i am making 2 or 3 updates to tables in seperate queries and if one fails i want them all rolled back. Can someone help me with the following:

Whats the difference between BEGIN and START TRANSACTION?

How can I best incorporate error handling eg. at present i'm running queries using the following code:

PHP Code:
$result mysql_query($query) OR die('Cannot perform query!'.mysql_error()); 
On a die statement does mysql look back for a BEGIN or START TRANSACTION and roll back to there or will I have to tell it to ROLLBACK?
captainmerton is offline  
Reply With Quote
Old 07-30-2009, 04:42 AM   #2 (permalink)
The Acquainted
 
JaoudeStudios's Avatar
 
Join Date: Jul 2009
Location: Surrey
Posts: 105
Thanks: 1
JaoudeStudios is on a distinguished road
Default

First of all are you using the correct database engine? InnoDb?

Also it is worth looking into MySQLi, it has many more features. Especially as MySQL api has been deprecated for sometime now!
__________________
JaoudeStudios.com | JaoudeStudios.com Forum | JaoudeStudios.com Blog
OpenSource is the road ahead...!
JaoudeStudios is offline  
Reply With Quote
Old 07-30-2009, 10:42 AM   #3 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

I'm running mysql 5 on my own machine installed myself - but i'll be deploying the app to a shared hosting site again mysql 5. Is the shared site likely to have innoDb installed? I believe a "SHOW ENGINES" command on the locally installed instance of mysql 5 will tell me whether I have it.

Do you have any links to good articles on MySQLi?
captainmerton is offline  
Reply With Quote
Old 07-30-2009, 10:48 AM   #4 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Is it worth using PDO?
captainmerton is offline  
Reply With Quote
Old 07-30-2009, 02:42 PM   #5 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

MySQLi could be more handy in this, since you are using mysql as a database, but PDO is also helpful as an ORM tool, you can use it to abstract what kind of database you are using.

There is a tutorial for transactions in MySQL that could explain it better then I could.I haven't use transactions in MySQL, it should be the same as others but better informed then sorry.

I found this tutorial with a simple example for MySQLi, it might help.

I don't know much about the subject (I should learn mysqli soon though :S), but I hope this helps.
tony is offline  
Reply With Quote
Old 07-30-2009, 03:02 PM   #6 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Thanks for that Tony. I dont think i need to think about database abstraction just now but will read up on MySQLi. Definitely need to use transactions in mysql and locking as well probably.
captainmerton is offline  
Reply With Quote
Old 07-30-2009, 03:15 PM   #7 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

Yeah I need to get better at my DB knowledge too.
tony is offline  
Reply With Quote
Old 07-30-2009, 05:16 PM   #8 (permalink)
The Acquainted
 
JaoudeStudios's Avatar
 
Join Date: Jul 2009
Location: Surrey
Posts: 105
Thanks: 1
JaoudeStudios is on a distinguished road
Default

The InnoDB is not just good for transactions but also row locking instead of table locking. This is why I favour InnoDB over MyIsam.
__________________
JaoudeStudios.com | JaoudeStudios.com Forum | JaoudeStudios.com Blog
OpenSource is the road ahead...!
JaoudeStudios is offline  
Reply With Quote
The Following User Says Thank You to JaoudeStudios For This Useful Post:
captainmerton (07-30-2009)
Old 07-30-2009, 06:24 PM   #9 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Well table locking is no use to me especially if i got decent traffic so i will look into InnoDB. Thanks for the advice guys.
captainmerton is offline  
Reply With Quote
Old 08-06-2009, 10:21 PM   #10 (permalink)
The Wanderer
 
Join Date: Aug 2009
Location: Pretoria, South Africa
Posts: 11
Thanks: 0
devnull is on a distinguished road
Default

Transactions are pretty easy to implement just execute the following in this order;

Code:
# Start the SQL Transaction
mysql_query("begin transaction");
Then execute any update/delete/insert queries

Code:
mysql_query("insert into......");
mysql_query("insert into......");
mysql_query("insert into......");
and finish off with

Code:
# Commit the changes
mysql_query("commit");
If any of the queries fail or a commit is not received(in the case of doing a die), the transaction(any queries executed) won't be committed to the database. Its as easy as that.
Send a message via Skype™ to devnull
devnull is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a MySQL class Andrew General 12 07-14-2009 03:49 PM
Securing your MySQL Queries with Sprintf Wildhoney General 26 03-18-2008 06:52 PM
MySQL Sell Up Alan @ CIT The Lounge 12 01-17-2008 05:46 PM
Error in connecting to MySQL via PHP EyeDentify MySQL & Databases 0 01-03-2008 01:06 PM
Notepage like application to open large MySQL files Wildhoney General 6 12-07-2007 02:18 PM


All times are GMT. The time now is 01:23 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design