0

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.

1
  • Beware SQL injections when putting user input in SQL queries. Commented Jul 14, 2013 at 17:23

1 Answer 1

2

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.")"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it works only if I reverse variables like: find_in_set($user_id, user_id) > 0

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.