Home arrow PHP arrow Page 3 - PHP Application Development With ADODB (part 2)

A Fear Of Commitment - PHP

In this concluding article, find out about ADODB's advancedfunctions, with examples that demonstrate how ADODB can be used tooptimize multiple-run queries, commit and roll back transactions,improve performance by caching query results, and automatically writeHTML (ortext) files.

TABLE OF CONTENTS:
  1. PHP Application Development With ADODB (part 2)
  2. Rapid Execution
  3. A Fear Of Commitment
  4. Cache Cow
  5. What's On The Menu?
  6. A Rose By Any Other Name...
  7. The Final Countdown
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 8
July 31, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
If your database system supports transactions (MySQL doesn't, but quite a few others do), you'll be pleased to hear that ADODB allows you to transparently use this feature in your scripts.

The following example demonstrates:

<?php // uncomment this to see plaintext output in your browser // header("Content-Type: text/plain"); // include the ADODB library include("adodb.inc.php"); // create an object instance // configure library for a MySQL connection $db = NewADOConnection("mysql"); // open connection to database $db->Connect("localhost", "john", "doe", "db278") or die("Unable to connect!"); // turn off auto-commit // begin transaction block $db->BeginTrans(); // first query $query = "INSERT INTO library (title, author) VALUES ('Title A', 'Author B')"; $result = $db->Execute($query) or die("Error in query: $query. " . $db->ErrorMsg()); // use ID from first query in second query if ($result) { $id = $db->Insert_ID(); $query = "INSERT INTO purchase_info (id, price) VALUES ($id, 'USD 39.99')"; $result = $db->Execute($query) or die("Error in query: $query. " . $db->ErrorMsg()); } // if no failures if ($result) { // commit $db->CommitTrans(); } // else rollback else { $db->RollbackTrans(); } // clean up $db->Close; ?>
The first step here is to turn off auto-committal of data to the database, via the BeginTrans() method; this method also marks the beginning of a transaction block, one which can be ended by either CommitTrans() or RollbackTrans(). Once auto-commit has been turned off, you can go ahead and execute as many queries as you like, secure in the knowledge that no changes have (yet) been made to the database.

Every call to Execute() within the transaction block returns either a true or false value, depending on whether or not the query was successful. These values can be tracked, and used to determine whether or not the entire transaction should be committed. Once you're sure that all is well, you can save your data to the database via a call to the CommitTrans() method. In the event that you realize you made a mistake, you can rewind gracefully with the RollbackTrans() function.

 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: