Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm about to build some new functionality on an old Drupal 6 site which will be upgraded to D7 or D8 within a year or so. This new module is heavy on the SQL and I'm building many complex SQL queries. I'd love to use PDO with named parameters to make the code readable.

Is it possible for me to initialize my own PDO connection to MySQL inside the module or will this break the rest of the Drupal environment somehow?

share|improve this question
up vote 2 down vote accepted

You definitely could (Drupal is just a a PHP application, nothing special in that respect). But you'd lose support for stuff like table prefixing.

If you're looking to use PDO, and stay within the bounds of normal Drupal practices, you should install the DBTNG module:

This is a backport of the Drupal 7 PDO database compatibility layer for use with Drupal 6 modules.


This module does not override the existing database functionality but makes the new compatibility layer available under a series of functions identical to their Drupal 7 counterparts named dbtng_ instead of db_. For example with this module, use dbtng_set_active instead of db_set_active, with the exception of the following functions that funciton identically to their Drupal 7 conterparts: db_insert, db_merge, db_update, db_delete, db_truncate, db_select, db_transaction, db_close, db_condition, db_xor, db_and, db_or, db_next_id, and db_like.

That should let you write code that won't need to be changed for Drupal 7 and 8 (unless table/field names change of course).

share|improve this answer
That sounds perfect, thanks! – Nic Aug 16 '14 at 10:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.