$sql = '
SELECT DISTINCT zip
FROM (
SELECT
zip,
(3958*3.1415926*sqrt((latitude-?)*(latitude-?) + cos(latitude/57.29578)*cos(?/57.29578)*(longitude-?)*(longitude-?))/180) as distance
FROM zip_codes
) r
WHERE
r.distance < ?
ORDER BY
r.distance
';
$stmt = $this->db->prepare($sql);
$stmt->bind_param('dddddd', $zip->latitude, $zip->latitude, $zip->latitude, $zip->longitude, $zip->longitude, $radius);
$stmt->bind_result($rowZip);
$stmt->execute();
$zip_codes = [];
while ($stmt->fetch()){
$zip_codes[] = $rowZip;
}
return $zip_codes;