Is there a convenient way to ask a particular database object to print its query?

Every now and then, I write a DBTNG db_op() in a module, and far to often, I don't quite get the queries right.

I know that devel can output the query log, but that's all queries, which, in this type of situation, is a problem more often than not.

I want to do

$result = db_select('node', 'n')->fields('n')->execute();

2 Answers 2


There is a dpq() function in the devel module which seems to fulfill this need.

Print a SQL string from a DBTNG Query object. Includes quoted arguments.

  • 5
    I'd give this +100 if I could, never seen that function before
    – Clive
    Commented Feb 21, 2012 at 12:27
  • 3
    You could have had, if you offered a bounty. ;)
    – apaderno
    Commented Feb 21, 2012 at 13:56

I know this is an old question but hopefully this answer might be useful to other people: basically Drupal core has its own DatabaseLog class. You could use it in cases where you don't have Devel module available (like maybe production debugging of slow queries?):

// do some SQL operations here...
// print the queries

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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