Search code examples
phpmysqldatabasedrupal-7

Select multiple rows from DB


I have the array with some IDs(all are unique). I want to select data from database for every id in array. I try this code, but its not working, where is my mistake ?

$array = ....;
foreach ($array as $key => $id) {
    $query = "SELECT * FROM user WHERE id = '$id'";
    $result = mysql_query($query);
    $rows = mysql_fetch_assoc($result)
}

Solution

  • Try this

    $array = ......;
    $id = implode(",", $array);
    $query = mysql_query("SELECT * FROM `user` where id IN($id)");
    while($row = mysql_fetch_array($query))
    {
        $user_id=$row['id'];
    }