从Post表格可能重复:
MySQL query using an array
How to use an array of values from PHP in the 'IN' clause of mysql query?
我有一个像这样的数组
Array
(
[userid] => Array
(
[0] => 4e8329e97231c
[1] => 4e64b47849318
[2] => 4e4e415a30000
)
)
我很难从这个数组中检索mysql中的用户数据
应该是这样的:
SELECT * FROM user_detail WHERE user_id='4e64b47849318' OR user_id='4e8329e97231c' OR user_id='4e4e415a30000'
答案 0 :(得分:7)
使用implode()
。
$yourArray = array_map("mysql_real_escape_string", $yourArray);
$query = "SELECT * FROM user_detail WHERE user_id='";
$query .= implode($yourArray, "' OR user_id='");
$query .= "'";
或者确实,使用SQL IN
关键字:
$yourArray = array_map("mysql_real_escape_string", $yourArray);
$query = "SELECT * FROM user_detail WHERE user_id IN ('";
$query .= implode($yourArray, "','");
$query .= "')";
答案 1 :(得分:2)
$clean_userid = array_map('mysql_real_escape_string', $arr['userid'])
$str_user_id = "'" . implode("', '", $clean_userid ) . "'";
$sql = "SELECT * FROM user_detail WHERE user_id IN ( $str_user_id )";
答案 2 :(得分:1)
你可以在这里很好地使用MySQL IN
运算符,它的工作方式类似于“OR”,但你基本上可以给它一个列表。
$user_id_string = implode(',', $array['userid']);
您现在有一个以逗号分隔的user_id字符串。
现在查询类似:
SELECT * FROM user_detail WHERE user_id IN ($user_id_string);
答案 3 :(得分:1)
$ criteria =“'”。implode(“','”,$ userID)。“'”;
$ sql =“select * from user_detail where user_id in($ criteria)”;
答案 4 :(得分:1)
你可以尝试
"SELECT * FROM user_detail
WHERE user_id IN ('". implode("','", $array['userid'])."')"
答案 5 :(得分:1)
$query="SELECT * FROM user_detail WHERE user_id='".(intval) $array['userid'][0]."' OR user_id='".(intval) $array['userid'][1]."' OR user_id='".(intval) $array['userid'][2]."'";