Ok,
I have an array of item ids that is placed into a PHP variable, through a while loop, such as...

while ...{
$array[$c] = $row->itemid;
$c++;
}

Then I need to do a SQL call and include those array elements in the WHERE statement. Currently I got around this by doing a for loop and generating a string (id=1 OR id=2 OR id=4,etc...).

This method seems a bit crude. Is there a more 'native' way of using arrays as part of the WHERE statement?

Thanks...
QuaffAPint

    I see that you are using OR in your sql query. If that is the case then why not just query an id in the range of the array? IE:

    ...WHERE id > $array[0]; AND id < $array[max]

    Would that solve your problem?

      Check out the SQL keyword IN, but you'll still need to build the string up in a loop. In general, SQL and arrays don't mix well.

      WHERE id IN (1,2,3,4)

        I can't do a 'range' of ids (1 to 5,etc..), because the arrary could be 1,2,3,8,31,52,etc...

        I might look at the IN option, at least it would be a little smaller resulting code.

        Thanks
        QuaffAPint

          Write a Reply...