1$lat = 41.118491 // user's latitude
2$lng = 25.404509 // user's longitude
3
4SELECT *,
5( 6371 * acos( cos( radians($lat) )
6* cos( radians( latitude ) )
7* cos( radians( longitude ) - radians($lng) ) + sin( radians($lat) )
8* sin( radians( latitude ) ) ) )
9AS calculated_distance
10FROM settings as T
11HAVING calculated_distance <= (SELECT distance FROM settings WHERE sid=T.sid)
12ORDER BY distance_calc
13