0

Can i put an array in mysql select ?

$usersArray = array(34, 23, 17, 17, 56);

$result = mysql_query("SELECT * FROM users WHERE user_id=$usersArray");

Can i do that ?

3 Answers 3

2

No. I believe this is how you should do that:

$result = mysql_query("SELECT * FROM users WHERE user_id IN (" . implode(', ', $usersArray) . ")");
Sign up to request clarification or add additional context in comments.

3 Comments

it verifies if a value (or field) is in a given set (which can be a select statement). Returns true on first match.
What can i do if i want search for 2 field like user_id and user_name
That's a separate question one would be pressed for space to answer in a comment.
0
$usersArray = array(34, 23, 17, 17, 56);

$result = mysql_query("SELECT * FROM users WHERE user_id in (".
    implode(",",$usersArray).
    ")");

This is unsafe, as in placing sql parameters as concatenated strings. But you get the general idea.

Comments

0

Try:

$usersArray = array(34, 23, 17, 17, 56);
$usersIn = implode(",",$usersArray);
$result = mysql_query("SELECT * FROM users WHERE user_id in($usersIn)");

The "in" does a comparison of user_id against any of the entries in the comma separated list within the brackets.

Comments

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.