0

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")->fields("mantenimiento_extras")->condition("id", $values, 'IN')->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")->condition("id", $values, 'IN')->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?

0

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.
}
| improve this answer | |
  • I had to change the query and not use the dynamic query since {mantenimiento_extras} was also not accepted. Even having more weird results when trying different alternatives, getting stuff like "SELECT FROM {mantenimiento_extras}" ... i fell back to hard-coding the query. But yes, that's another issue i got in my query. – Luis Masuelli Mar 14 '14 at 15:40

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.