Searching API docs for an hour now. How can I get the string that db_select is building and sending to the db?

Comments

dave reid’s picture

Status: Active » Fixed

If you have the $query variable, then you can do something like drupal_set_message((string) $query) before you run $query->execute(). Otherwise you can download and enable the Devel module and call dpq($query).

marcusx’s picture

dpq($query)! Thats's cool. Didn't know it exists. Thanks a lot!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

apotek’s picture

A different way, just for completeness:

// Get query string:
echo $query->__toString() . "\n";
// Get the arguments passed to the string
$args = $query->getArguments();
var_dump($args);

Of course, if you have devel etc, dpq is great, but just wanted to document some more basic, underlying methods.

geigel’s picture

Issue summary: View changes

Thanks apotek -- exactly what I was looking for.

shadab ahmad’s picture

You can use dpq() function to return actual SQL query which you can directly execute in MySQL.

To use dpq() please install devel (https://www.drupal.org/project/devel) module.

Example below:

1- Go to your_base_url/devel/php

2- Print query like this.

$query = db_select('node', 'n')
    ->fields('n')
    ->condition('nid','2','=');

dpq($query);