Sotiris
Sotiris

Reputation: 40046

select rows depending on an array values

I have an array which populated by a cookie. The length differ each time. The values that contains are ids taht I want to select from a database. I know that I can use something like below to retrieve multiple rows with specific ids:

$query = "SELECT game_id,thumb,title,rating,instructions FROM games WHERE game_id IN ('111','110')";

My question is, how can I retrieve from the database the ids of the array $favGames ? I found in another question the following: WHERE id IN (' . implode(',', $ids) . ')'; but it doesn't seems to works for me. What other options do I have?

Upvotes: 0

Views: 151

Answers (2)

Harper Shelby
Harper Shelby

Reputation: 16585

You'll need something like this:

$query = "SELECT game_id,thumb,title,rating,instructions FROM games WHERE game_id IN ('" . implode("','", $favGames) ."')";

If you don't have the extra single quotes in the call to implode, your query would look like:

SELECT game_id,thumb,title,rating,instructions FROM games WHERE game_id IN ('111,110')

Notice the missing quotes? That's what's causing your error.

Upvotes: 1

Mauro
Mauro

Reputation: 599

shouldn't $ids be $favGames in your second expression? And are all ids numeric? If they are not, then you need to add '' around the elements just like in your initial query

Upvotes: 0

Related Questions