drupal 7, db_select(), strange behavior : malformed query I have a table: mantenimiento_extras.  

I want a query like this: select * from mantenimiento_extras where id in (7, 9).
(this case assumes passing array(7, 9), but it could be variable, even including the case for "in ()".  

I execute this query:

db_select("mantenimiento_extras")-&gt;fields("mantenimiento_extras")-&gt;condition("id", $values, 'IN')-&gt;execute();//$values is guaranteed to be an array.


I get this query, and an error:

SELECT mantenimiento_extras.* FROM {mantenimiento_extras} mantenimiento_extras WHERE (id IN ()); Array( )


I modify the dynamic query:  

db_select("mantenimiento_extras")-&gt;condition("id", $values, 'IN')-&gt;execute();  


I get this query, and an error:  

SELECT FROM {mantenimiento_extras} mantenimiento_extras WHERE (id IN ()) ; Array ( )


Now I bang my head against the nearest object in my desk.  

Why is it generating malformed queries?
If the array is empty, you'll get WHERE foo IN (). That's invalid SQL syntax.

To prevent, use a conditional:

if (!empty($values)) {
  // Run the query.
}