SELECT *, ( 6371 * acos( cos( radians('.floatval($lat).') ) * cos( radians( X(p.coord) ) ) * cos( radians( Y(coord) ) - radians('.floatval($lng).') ) + sin( radians('.floatval($lat).') ) * sin( radians( X(p.coord) ) ) ) ) as distance
FROM `my_table`
WHERE MBRWithin(`coordinates`, GeomFromText('Polygon(($sq[0][0] $sq[0][1], $sq[1][0] $sq[1][1],$sq[2][0] $sq[2][1], $sq[3][0] $sq[3][1], $sq[4][0] $sq[4][1]))'))
HAVING distance <= $rdeg*$rdeg
ORDER BY distance
$lat = $q[0]; $lng = $q[1]; $r = 3; // km
SELECT ...
HAVING ( 6371 * acos( cos( radians('.floatval($lat).') ) * cos( radians( X(p.coord) ) ) * cos( radians( Y(coord) ) - radians('.floatval($lng).') ) + sin( radians('.floatval($lat).') ) * sin( radians( X(p.coord) ) ) ) ) < $r
SELECT * FROM `my_table`
WHERE MBRWithin(`coordinates`, GeomFromText('Polygon(($sq[0][0] $sq[0][1], $sq[1][0] $sq[1][1],$sq[2][0] $sq[2][1], $sq[3][0] $sq[3][1], $sq[4][0] $sq[4][1]))'))
HAVING X(coordinates)*X(coordinates) + Y(coordinates)*Y(coordinates) <= $rdeg*$rdeg
$kmInDeg = LatLngDist($q,array($q[0],$q[1]+1));
$degInKm = 1 / $kmInDeg;
$r = 3; // radius = 3km
$rdeg = $r * $degInKm;
$sq[0] = array($p[0]-$rdeg,$p[1]+$rdeg); // левый верхний
$sq[1] = array($p[0]+$rdeg,$p[1]+$rdeg); // правый верхний
$sq[2] = array($p[0]+$rdeg,$p[1]-$rdeg); // правый нижний
$sq[3] = array($p[0]-$rdeg,$p[1]-$rdeg); // левый нижний
$sq[4] = $sq[0]; // замыкаем полигон