I have searched the Web a lot to find the solution for my problem but I still can't figure it out.
I have a very simple database with id, city name, latitude, longitude and city_info. When someone enters a city page a would like to show the 10 nearby cities.
How can I calculate this with MySQL and return it with PHP?
I have seen a lot of suggestions on this website, however none of these work somehow.
What I tried without success. I do not get any results.
<?php
$slatitude = 43.2141341;
$slongitude = 64.4368684;
$miles = 200;
//connect
$query = "SELECT *,
( 3959 * acos( cos( radians('$slatitude') ) *
cos( radians( latitude ) ) *
cos( radians( longitude ) -
radians('$slongitude') ) +
sin( radians('$slatitude') ) *
sin( radians( latitude ) ) ) )
AS distance FROM cities HAVING distance < '$miles' ORDER BY distance ASC LIMIT 0, 10";
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$cityname = $row['cityname'];
$latitude = $row['latitude'];
$longitude = $row['longitude'];
echo "$cityname<br />";
}}
?>`