开发者

Can i put an array in mysql select?

开发者 https://www.devze.com 2023-01-24 12:51 出处:网络
Can i put an array in mysql select ? $usersArray = 开发者_如何学运维array(34, 23, 17, 17, 56);

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 ?


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

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


$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.


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.

0

精彩评论

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