Book Image

Learning PHP Data Objects

By : Dennis Popel
Book Image

Learning PHP Data Objects

By: Dennis Popel

Overview of this book

PDO is lighter, faster, and more powerful than existing PHP data abstraction interfaces. PDO is a common interface to different databases that must be used with a database-specific PDO driver to access a particular database server: the PDO extension does not provide a database abstraction by itself; it doesn't rewrite SQL, emulate missing database features, or perform any database functions using by itself. It performs the same role as other classic database abstraction layers such as ODBC and JDBC: it's a query abstraction layer that abstracts the mechanism for accessing a database and manipulating the returned records; each database driver that implements the PDO interface can also expose database-specific features as regular extension functions. ¬ PDO ships with PHP 5.1, and is available as an extension for PHP 5.0; it requires the new object-oriented features of PHP 5, and cannot run with earlier versions of PHP.This book will teach you how to use the PDO, including its advanced features. Readers need to be aware of the basics of data abstraction and should be familiar with PHP.
Table of Contents (13 chapters)

Transactions


PDO API also standardises the transaction handling methods. By default, after the successful creation of the PDO connection, it is set to autocommit mode. This means that for every database that supports transactions, every query is wrapped in an implicit transaction. For those database that do not support transactions, every query is executed as is.

Typically, the transaction handling strategy is this:

  1. 1. Begin the transaction.

  2. 2. Wrap the database-related code in a try...catch block.

  3. 3. The database-related code (within the try block) should commit the changes after all the updates have been done.

  4. 4. The catch block should rollback the transaction.

Of course, only the code that updates the database and the code that can break data integrity should be handled in a transaction. A classic example of a transaction is a money transfer:

  1. 1. Begin the transaction.

  2. 2. If there is enough money on the payer's account:

    • Subtract the amount from the payer's account.

    • Add the amount to the beneficiary...