Get all records nearby locations with latitude and longitude from database in particular kilometer radius

A query to get this is given below:

$latitude = '35.985';
$longitude = '-48.45';
$kilometer = '10';

$query = "SELECT *,( 3959 * acos( cos( radians(" . $latitude . ") ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(" . $longitude . ") ) + sin( radians(" . $latitude . ") ) * sin( radians( latitude ) ) ) ) AS distance FROM tbl_hotels HAVING distance < " . $kilometer . " ORDER BY distance";

NOTE: "latitude" & "longitude" are the column name of tbl_hotels

Above query will display hotels available in the radius of 10KM from given latitude and longitude.

Enjoy…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s