When troubleshooting queries built with Drupal 7’s Database API (affectionately known as “DBTNG”), there often comes a time when I need to see the actual query that’s going to be executed in the database. Here’s a quick and handy tip to assist query troubleshooting.
Suppose you have a query with arguments, like the following:
$query = db_select('node', 'n') ->fields('n') ->condition('n.type', 'page');
You can very easily see the contents of this query by using dbq:
Essentially, it uses the the query object’s __toString method to get the stringified version of the query, and additionally injects the escaped arguments into the query.