0

I have an array with these values, 1 & 5 and I am trying to generate a mysql query to return all of these values id, I hope that makes sense, here is my code:

if(isset($filterOpts['location'])){
                        foreach($filterOpts['location'] as $row => $value){
                                $where .= " AND `rb_locations`.`locationId` = " . $value;
                        }
                }

this will return AND rb_locations.locationId = 1 AND rb_locations.locationId = 5

I am guess that AND and AND are causing my problems. how do I fix this? When I remove the 5, it returns results.

I didnt post the rest of the query because I know its not the issue :)

Any suggestions on how to fix this?

0

1 Answer 1

3

Try using the IN clause. It checks whether a value is within a set of values:

if (isset($filterOpts['location'])) {

   $locationIds = implode(',', $filterOpts['location']);

   $where .= " AND `rb_locations`.`locationId` IN (" . $locationIds . ") ";

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.