PCHomepage
Programmer
I am trying to pull up only the smallest value for distance between locations. The locations table has only a few entries so speed is not an issue and "as the bird flies" is close enough. I am feeding the query an external location and I want only the single one that is closest. This query below as it shown is giving an array containing the proper distance but the other fields are from first row entry.
When I remove the MIN() and add GROUP BY Distance it seems to work but I am not sure why or if that is the proper way to do it. Please advise.
When I remove the MIN() and add GROUP BY Distance it seems to work but I am not sure why or if that is the proper way to do it. Please advise.
Code:
// Customer's location
$center_lat = 36.713171;
$center_lng = -121.624309;
// My locations
$query = sprintf("SELECT ID, Address, MIN((3959 * acos(cos(radians('%s')) * cos(radians(lat)) * cos(radians(lng) -
radians('%s')) + sin(radians('%s')) * sin( radians(lat))))) AS Distance
FROM locations
LIMIT 1",
$center_lat,
$center_lng,
$center_lat);
$rowCat = DBConnect($query, "Select", "pchome_geoip");
echo "<pre>";
print_r($rowCat);
echo "</pre>";