8

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();
dpm($result->queryWithParameters);
dpm($result->finalQuery);
13

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 Feb 21 '12 at 12:27
  • 3
    You could have had, if you offered a bounty. ;) – kiamlaluno Feb 21 '12 at 13:56
2

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?):

Database::startLog('my_custom_key');
// do some SQL operations here...
// print the queries
print_r(Database::getLog('my_custom_key'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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