Použijte pole:
$ids = array();
foreach($result as $key=>$value){
$ids[] = $value['id'];}
if (count($ids) > 0) // to avoid mySQL error on empty array
{
$ids_string = implode(",", $ids);
$sql2 = 'SELECT * FROM table where id IN($ids_string)';
}