Home arrow PHP arrow Page 4 - Working with Prepared Queries with PDO Objects in PHP 5

Working with transactions - PHP

If you're a PHP developer who builds web applications that interact with different database systems, such as MySQL, Oracle, MS SQL, and so forth, then this group of articles might be what you're looking for. Welcome to the final part of the series that began with "Using PDO objects in PHP 5." This series shows you how to implement the most important methods that come packaged with the PDO database abstraction layer (short for PHP Data Objects).

TABLE OF CONTENTS:
  1. Working with Prepared Queries with PDO Objects in PHP 5
  2. Working with prepared queries
  3. Using an alternate approach with prepared queries
  4. Working with transactions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
June 11, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Any database abstraction layer that fits the requirements of modern web applications must support transactions. The PDO extension is no exception. It comes packaged with a neat set of methods aimed specifically at dealing with this important feature.

Basically, the library includes three simple methods for working with transactions, called "beginTransaction()," "commit()" and "rollBack()" respectively. All of them are very easy to follow. Below I coded two hands-on examples that demonstrate their functionality, at least primitively.

Here are the corresponding code samples:

// example using 'beginTransaction()', 'rollback()' and 'commit
()' methods
try{
  
$dbh=new PDO('mysql:host=localhost;dbname=alejandro','user','password');
  
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
$dbh->beginTransaction();
  
$dbh->exec("INSERT INTO users (id,name,address,email) VALUES
(NULL,'Lindsay Wagner','1234 Binary
Avenue','lindsay@domain.com')");
  
$dbh->exec("INSERT INTO users (id,name,address,email) VALUES
(NULL,'Jeff Wilson','666 Parsed Boulevard','jeff@domain.com')");
  
$dbh->commit();
}
catch(PDOException $e) {
  
$dbh->rollBack();
  
echo 'Error : '.$e->getMessage();
}

// additional example using 'beginTransaction()', 'rollback()'
and 'commit()' methods
try{
  
$dbh=new PDO
('mysql:host=localhost;dbname=alejandro','user','password');
  
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
$dbh->beginTransaction();
  
$dbh->exec("INSERT INTO users (id,name,address,email) VALUES
(NULL,'Susan Norton','1234 Jointner
Avenue','lindsay@domain.com')");
  
$dbh->exec("UPDATE users SET name='Jim Smith',address='1010
Main Street',email='jim@domain.com' WHERE id=5");
  
$dbh->commit();
}
catch(PDOException $e) {
  
$dbh->rollBack();
  
echo 'Error : '.$e->getMessage();
}

As you can see, the above hands-on examples utilize the group of transaction-related methods that I introduced previously, to execute different queries. In the first case, two insertions are performed against the same "USERS" database table that you saw in earlier sections, while in the second case a new row is added to the table in question, and then an existing record is updated. Quite simple, isn't it?

All right, at this moment you should have a better idea of how to use the PDO extension that comes bundled with PHP 5.1 and up. As you saw, it features plenty of handy methods that can be used with a decent variety of database systems.

Naturally, you have the liberty to experiment with all the code samples shown here, with the firm purpose of improving your background in this powerful PHP-based database abstraction layer.

Final thoughts

Sadly, we've come to the end of this series. As you hopefully learned, the PDO extension can be really helpful if you're developing web applications that talk to different database systems.

Also, it's fair to say that this series is intended to be a simple introduction to the library's main features, and certainly doesn't cover other additional characteristics. If you're looking for thorough documentation on it, the PHP official site is the best place to go.

See you in the next PHP tutorial!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

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: