O'Reilly logo

Drupal 7 Module Development by Ken Rickard, John Wilkins, Larry Garfield, Matt Butcher, Greg Dunlap, Matt Farina

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Dynamic queries

Although most SELECT queries are static, at times we will need a more flexible query. That could be because the query itself may change depending on incoming user data, because we want to allow other modules to modify our query before it is executed, or we want to take advantage of some database feature that is implemented differently on different databases. For these cases, Drupal provides a mechanism for building dynamic queries using a robust query builder.

To start, we create a new query object with db_select():

$query = db_select('node', 'n');

The first parameter is the name of the base table of the query and the second is the alias we want to use for it. We then call additional methods on the $query object in order to build ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required