嘿家伙,谢谢你的帮助。 我想要做的是使用用户的纬度和经度,并将其与我在我的数据库中保存的经度和纬度与相关城市名称进行比较。我现在的代码工作正常,但问题是半径30英里内可能有多行。看看我的代码,如果可以的话,帮助我。PHP mysqli_fetch_array选择最小值
<?php function distance($lat1, $lon1, $lat2, $lon2) {
$pi80 = M_PI/180;
$lat1 *= $pi80;
$lon1 *= $pi80;
$lat2 *= $pi80;
$lon2 *= $pi80;
$r = 6372.797; // mean radius of Earth in km
$dlat = $lat2 - $lat1;
$dlon = $lon2 - $lon1;
$a = sin($dlat/2) * sin($dlat/2) + cos($lat1) * cos($lat2) * sin($dlon/2) * sin($dlon/2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
$km = $r * $c;
//echo '<br/>'.$km;
$km = $km * 0.621371;
return $km;
}
include_once("dbconnect.php");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$grabCities = "SELECT * FROM cities";
$result = $mysqli->query($grabCities);
while($row = mysqli_fetch_array($result)){
$distance = distance(41.08866717,-81.4780426,$row['lat'],$row['longitude']);
if ($distance < 30){
echo $myCity = $row['City'] . "<BR>";
}
}
$mysqli->close();
?>
这真的不清楚你在这里问什么。 – Augwa
我想要做的是从我在我的数据库中获得的所有结果;转换经度和纬度。找到距离用户最近的城市,并选择该城市。 @Augwa –
您可以在SQL中执行此操作,并将结果限制为1. – Augwa