Mike
Mike

Reputation: 171

php mysql where IN clause

I have a simple query:

$user_id = $_SESSION['user_id'];

SELECT *  FROM pages WHERE user_id IN($user_id);

field user_id in pages tables has the following format 1,3,5,... it contains multiple user_id

What I need is to select all rows based on logged in user_id. The above attempt does not works, it only picks up the first number.

Upvotes: 0

Views: 3163

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270773

You need to use find_in_set():

select *
from pages
where find_in_set(user_id, $user_id) > 0;

Alternatively, you can construct the SQL so it has the values in the string. Something like:

select *
from pages
where user_id in (".$user_id.")"

Upvotes: 2

Related Questions