I am writing a query that will retrieve nodes that match a set of criteria. Though the number of cirteria is unknown. I was hopping i could pass to the condition() method an array of criteria with a key as the 'column name' and that value as the 'value to match' for that colum. For instance :

$criteria = array ('nid' => 1, 'etid' => '1')

$query = db_select('table', 't');

  ->condition( $criteria)
  ->fields('t', some_field);

I have almost 10 criteria and no way to know which will be selected so writing a query for each wont be possible.

Is there a way to achieve that ?


i thought about using that :

if (empty($criteria1)){
    $criteria = array (":criteria1"= '*');
}else {
 $criteria = array (":criteria1"=$criteria1);
$query = db_query('SELECT nid FROM { parce_index } WHERE criteria1 = :criteria1 AND criteria2= criteria2...',$criteria));

Would that make sense ?

1 Answer 1


If anyone ever need that i got an answer there.


This is useful as well :


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.