Working with Prepared Queries with PDO Objects in PHP 5 - Working with transactions
(Page 4 of 4 )
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!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |