0

At where clause i am getting one id on $values variable, while there are four id exist in $rest_id. How to fix this problem that i could get all the four ids in where clause. Anyone please help to fix this issue please. p.

foreach ($rest_id as $value) 
{
$ids = array($value->id);
$nearest_rest = "SELECT *, (3956 * 2 * ASIN(SQRT( POWER(SIN(( 28.5812674 - lat) * pi()/180 / 2), 2) +COS( 28.5812674 * pi()/180) * COS(lat * pi()/180) * POWER(SIN(( 77.3181059 - lng) * pi()/180 / 2), 2) ))) as distance 
                   FROM restaurant_details 
                  WHERE id In ('" . implode("','",$ids) . "') 
                 HAVING distance 
               ORDER BY distance ASC 
                  LIMIT 1"; 
}
Mahmut Ali ÖZKURAN
  • 1,120
  • 2
  • 23
  • 28
prince
  • 11
  • 1

2 Answers2

0
// first - take all ids to `$ids` array:
$ids = array();
foreach ($rest_id as $value) {
    $ids[] = $value->id;
}
// then do your query:
$nearest_rest = "SELECT *, (3956 * 2 * ASIN(SQRT( POWER(SIN(( 28.5812674 - lat) * pi()/180 / 2), 2) +COS( 28.5812674 * pi()/180) * COS(lat * pi()/180) * POWER(SIN(( 77.3181059 - lng) * pi()/180 / 2), 2) ))) as distance from restaurant_details where id In ('" . implode("','",$ids) . "') having distance order by distance asc limit 1"; 
u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

Hello to use array of ids in the query please try below solution.

foreach ($rest_id as $value) {
    $ids[] = $value->id;
}

//Than now write your query this way
$nearest_rest = "SELECT *, (3956 * 2 * ASIN(SQRT( POWER(SIN(( 28.5812674 - lat) * pi()/180 / 2), 2) +COS( 28.5812674 * pi()/180) * COS(lat * pi()/180) * POWER(SIN(( 77.3181059 - lng) * pi()/180 / 2), 2) ))) as distance from restaurant_details where id In (" . implode(",",$ids) . ") having distance order by distance asc limit 1"; 
Hardik Patel
  • 706
  • 5
  • 14