Drupal 7 - Working with DB via PHP PDO
With the transition to Drupal 7, we move to the new Drupal DB abstraction layer API, which is built on top of PDO. PDO has long been used in frameworks like Zend, as well as many other PHP frameworks. But everything seemed so convenient in Drupal 6 with writing plain SQL queries — so why do we need something new?
Let’s first understand what PDO is.
PDO allows PHP code for working with one database to be ported to another database without major changes. For example, if your site was using MySQL, you could switch to PostgreSQL with minimal adjustments. I haven’t tried using it with Oracle yet, but it’s expected to work there too.
PHP Data Objects (PDO) is a PHP extension that provides a lightweight, consistent interface for accessing databases. Each database driver is implemented through the PDO interface and may also include server-specific features for each DB server.
PDO provides access to a database abstraction layer, which in turn is used to handle queries and represent data. However, PDO does not provide full database abstraction — it does not rewrite SQL or emulate missing features of specific database servers. If you need such features, you’ll need to use a full-featured abstraction layer instead. PDO is compatible with PHP 5.1 and above, and is available as a PECL extension for PHP 5. It requires the new object-oriented features introduced in PHP 5 and is not supported in earlier PHP versions.
This means developers can write cross-platform database code more easily. PDO is not an abstraction layer like PearDB. Rather, PDO is a consistent DB access interface rather than a high-level API.
If we want to write modules for Drupal 7, we must use the PDO-based syntax for writing database queries to ensure our modules can run on a variety of database engines.