Searching API docs for an hour now. How can I get the string that db_select is building and sending to the db?
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).
dpq($query)! Thats's cool. Didn't know it exists. Thanks a lot!
Automatically closed -- issue fixed for 2 weeks with no activity.
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.
Thanks apotek -- exactly what I was looking for.
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);
Comments
Comment #1
dave reidIf 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).
Comment #2
marcusx commenteddpq($query)! Thats's cool. Didn't know it exists. Thanks a lot!
Comment #4
apotek commentedA different way, just for completeness:
Of course, if you have devel etc, dpq is great, but just wanted to document some more basic, underlying methods.
Comment #5
geigel commentedThanks apotek -- exactly what I was looking for.
Comment #6
shadab ahmad commentedYou 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.