1

I have this string in PHP:

$str = 1,3,6,5 

These numbers can change and the length of the set can change to more or less than four. I want to convert to a MySQL query like this in PHP:

$qry = mysql_query("SELECT * FROM tbl WHERE id = 1 or id = 3 or id = 6 or id = 5")

It should be easy, but I don't know how to do it.

I will be grateful for any help.

1
  • preg_split("/,/", $str); Commented Sep 20, 2012 at 2:50

1 Answer 1

5

You could use IN.

$query = mysql_query("SELECT * FROM table WHERE id IN ($str)");

Make sure $str is validated first to prevent sql injection.

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

1 Comment

Would having a loop casting all values to int be sufficient ?

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.