I have a table of users, and the geo-location of each user is stored as a pair of longitude and latitude.
I would like to show all the users within a circular area, with known center and radius. The following code is what I have so far, but it does not give the correct answer:
$lat = 54.635214;
$lon = -3.551677;
$bounding_distance = 10;
$sql = "SELECT *
,((ACOS(SIN(:find_lat * PI() / 180) * SIN(`latitude` * PI() / 180) + COS(:find_lat * PI() / 180) * COS(`latitude` * PI() / 180) * COS((:find_long - `longitude`) * PI() / 180)) * 180 / PI()) * 60 * 1.15078) AS `distance`
FROM `user`
WHERE
`latitude` BETWEEN (:find_lat - :bounding_distance) AND (:find_lat + :bounding_distance)
AND `longitude` BETWEEN (:find_long - :bounding_distance) AND (:find_long + :bounding_distance)
AND `distance` <= 100
ORDER BY `distance` ASC";
$stmt = $this->pdo->prepare($sql);
$stmt->bindValue(':find_lat', $lat);
$stmt->bindValue(':find_long', $lon);
$stmt->bindValue(':bounding_distance', $bounding_distance);
$stmt->execute();
return $stmt->fetchAll();