您好我目前正在开发一个基于地理位置的应用程序,它会更新用户的位置并显示他周围的商店,我在 sql 数据库中使用普通的 sql 查询。我的 php 代码选择用户周围的商店是:
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
if (isset($_POST['code']) && isset($_POST['lat']) && isset($_POST['lng'])) {
$code = $_POST['code'];
$lng = $_POST['lng'];
$lat = $_POST['lat'];
// 从表中获取所有数据这是我在需要更改的地方获取数据的代码
$result = mysql_query("SELECT *, ( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians(lng) - radians($lng) ) + sin( radians($lat) ) * sin( radians(lat)))) AS distance
FROM maintable
HAVING distance < 25
ORDER BY distance
") or die(mysql_error());
// 检查空结果
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["people"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$people = array();
$product["_id"] = $row["_id"];
$product["name"] = $row["name"];
$product["distance"] = $row["distance"];
$product["lat"] = $row["lat"];
$product["lng"] = $row["lng"];
$product["image_bit"] = $row["image_bit"];
$product["security"] = $row["security"];
$product["status"] = $row["status"];
$product["code"] = $row["country_code"];
$product["phone"] = $row["phone"];
// push single product into final response array
array_push($response["people"], $people);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No people found";
// echo no users JSON
echo json_encode($response);
}
}
else{
//required field is missing
$response["success"] = 0;
$response["message"] = "Required field missing";
// echoing JSON response
echo json_encode($response);
}
?>
我将用户的纬度和经度值分别存储为 lat 和 lng。但我最近听说 sql 中的空间数据类型,这将更有效,我需要知道的是,如果我需要使用空间查询,应该在 sql 表中的 lat 和 lng 列中进行哪些更改,并且可以请任何人修改 mysql 查询作为空间查询。任何帮助都将是可观的