我有一个充满位置的MySQL数据库,我希望编写一个接受纬度和经度作为参数的php页面,并查询位置的MySQL数据库,其结果在经纬度的+/- 0.01以内。 / p>
<?php include "../inc/dbinfo.inc";
$lat = $_GET['lat'];
$lon = $_GET['lon'];
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();
$database = mysqli_select_db($connection, DB_DATABASE);
settype($lat, "double");
settype($lon, "double");
$lat_max = bcadd($lat, 0.01);
$lat_min = bcsub($lat, 0.01);
$lon_max = bcadd($lat, 0.01);
$lon_min = bcsub($lat, 0.01);
if ($lat_min > $lat_max) {
$temp = $lat_min;
$lat_min = $lat_max;
$lat_max = $temp;
}
if ($lon_min > $lon_max) {
$temp = $lon_min;
$lon_min = $lon_max;
$lon_max = $temp;
}
$qstring = "SELECT * FROM toilets WHERE (Latitude BETWEEN " . $lat_min . " AND " . $lat_max. ") AND (Longitude BETWEEN " . $lon_min . " AND " . $lon_max . ")";
$result = mysqli_query($connection, $qstring);
$myArray = array();
if ($result) {
$tempArray = array();
while($row = $result->fetch_object()) {
$tempArray = $row;
array_push($myArray, $tempArray);
}
echo json_encode($myArray);
}
mysqli_free_result($result);
mysqli_close($connection);
?>
由此产生的MySQL select语句不是预期的结果。 我相信问题在于PHP中的浮点精度,但是我尝试使用bcadd和bcsub却没有运气。有人可以指出我在代码中出错的地方,还是我对PHP处理浮点数的理解的地方? 谢谢