Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

PHP + MySQL Commit?

SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
Watch

xrvel

i love automationVIP Member
Impact
165
I have several MySQL queries which must be done perfectly on one execution.
I heard about "transaction" which supports "commit" and "rollback" or such.

But i don't know what is the PHP code to do this.
I heard about Pear DB but i read on their website that it is somewhat "outdated".

Does anyone know what code should i use?
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
To use transactions, you just need to execute the right queries from your PHP script (obviously make sure your database supports transactions):

PHP:
mysql_query("START TRANSACTION");
mysql_query("UPDATE t1 SET field='value1'");
mysql_query("UPDATE t2 SET field='value2'");
mysql_query("COMMIT");

If your requirement is that you don't want other MySQL processes reading part modified tables, it may be better to use a table write lock, like this:

PHP:
mysql_query("LOCK TABLES t1 WRITE, t2 WRITE");
mysql_query("UPDATE t1 SET field='value1'");
mysql_query("UPDATE t2 SET field='value2'");
mysql_query("UNLOCK TABLES t1, t2");

No other MySQL process can access the t1 or t2 tables whilst they are locked.

Alternativly, if your transactions are simple, combine them into a single MySQL transaction. For example:

PHP:
mysql_query("UPDATE t1, t2 SET t1.field='value1', t2.field='value2'");
 
Last edited:
1
Close
Thanks, i didn't know that before :tu:
I'll try it for sure :tu:
 
0
Close
If you have mySQL 5+ have a look at stored procedures.
 
0
Close
CatchedCatched

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Search: better matches first
Search now shows better matches first. Threads with your words in their title now come near the top. A poll with your words in its...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back