我有一些带有一些ID的数组(都是唯一的)。我想从数据库中为数组中的每个id选择数据。我试试这段代码,但它不起作用,我的错误在哪里?
$array = ....;
foreach ($array as $key => $id) {
$query = "SELECT * FROM user WHERE id = '$id'";
$result = mysql_query($query);
$rows = mysql_fetch_assoc($result)
}
答案 0 :(得分:2)
试试这个
$array = ......;
$id = implode(",", $array);
$query = mysql_query("SELECT * FROM `user` where id IN($id)");
while($row = mysql_fetch_array($query))
{
$user_id=$row['id'];
}
答案 1 :(得分:-2)
我想提出另一种解决方案,试试吧:
$query = "SELECT * FROM user WHERE 1=1 ";
if(count($array){
$query .= " AND (";
foreach ($array as $key => $id) {
$query .= ' OR id ='.$id;
}
$query .= ")";
}
$result = mysql_query($query);
我希望这可以帮到你。