开发者

select rows depending on an array values

开发者 https://www.devze.com 2023-02-11 12:38 出处:网络
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

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 开发者_StackOverflow('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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消