PHP MySql Radius Search WebService – Source

Sometimes you need your mysql radius-search backend fast! This is how I do it on the fly. For more infos regarding radius search check this link.


<?php
$conn = mysql_connect("SERVER_IP:HOST", "YOUR_USERNAME", "PWD_") or die(mysql_error());
mysql_select_db("YOUR_DB_NAME") or die(mysql_error());

$latitude = $_GET['lat'];
$longitude = $_GET['lng'];
$radius = $_GET['r'];

// rating minimum
$rmin = $_GET['minr'];
// rating maximum
$rmax = $_GET['maxr'];

$query = sprintf("SELECT street, tvg_name_hash, tvg_rating,b_stars,long_name, lat, lng,
		( 6371 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) +
		sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance
		FROM hotel WHERE hotel.b_stars<=3 AND hotel.tvg_rating>='%s' AND hotel.tvg_rating<='%s' HAVING distance < '%s'  ORDER BY distance LIMIT 0 , 50",
                mysql_real_escape_string($latitude),
                mysql_real_escape_string($longitude),
                mysql_real_escape_string($latitude),
                mysql_real_escape_string($rmin),
                mysql_real_escape_string($rmax),
                mysql_real_escape_string($radius));

$result = mysql_query($query); 

if (!$result) { die("Invalid query: " . mysql_error()); }

// Iterate through the rows, adding XML nodes for each
$output=array();
while ($row = mysql_fetch_assoc($result)){
 $output[$row['tvg_name_hash']][] = array(
          'name' => utf8_encode($row['long_name']),
	  'trating' => $row['tvg_rating'],
	  'bstars' => $row['b_stars'],
	  'lat' => $row['lat'],
      'lng' => $row['lng']
   );
}

// JSON output
echo json_encode($output);

// close connection
mysql_close($conn);
?>;

Enjoy the source and have fun…

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

%d bloggers like this: