我在准备好的声明中返回$ distance时遇到了一些问题。 $ station_id被呼出,一切都很好,但距离是空的。
我还替换了问号并直接在phpmyadmin中运行查询,它返回distance和station_id列,以便查询本身显示正常。
难以弄清楚原因,并想知道是否有人可以解决出错的问题。请找到下面的代码,如果您有任何疑问,请不要犹豫。哦,对于背景,sql公式是haversine formula
非常感谢任何帮助,再次感谢。
<?php
require_once("../includes/station.php");
require_once("../includes/maps.php");
global $conn;
$latitude = 54.335;
$longitude = -1.45007;
//$distance = 50;
$limit_start = 0;
$limit_end = 5;
//$query = "SELECT *,(((acos(sin((".$latitude."*pi()/180)) * sin((`Latitude`*pi()/180))+cos((".$latitude."*pi()/180)) * cos((`Latitude`*pi()/180)) * cos(((".$longitude."- `Longitude`)*pi()/180))))*180/pi())*60*1.1515) as distance FROM `postcodelatlng` having distance <= ".$distance;
//$query .= ' order by distance asc limit 20, 40';
//works
//SELECT station_details.station_id, (((acos(sin((54.335*pi()/180)) * sin((`Latitude`*pi()/180))+cos((54.335*pi()/180)) * cos((`Latitude`*pi()/180)) * cos(((-1.45007- `Longitude`)*pi()/180))))*180/pi())*60*1.1515) as distance FROM `postcodelatlng`inner join station_details on station_details.post_code = postcodelatlng.postcode
// Query uses the haversine formula to calculate distance in miles as the crow flies
$query = "SELECT station_details.station_id,
(((acos(sin((?*pi()/180)) * sin((`Latitude`*pi()/180))+cos((?*pi()/180)) * cos((`Latitude`*pi()/180)) * cos(((? - `Longitude`)*pi()/180))))*180/pi())*60*1.1515) AS distance
FROM `postcodelatlng`
INNER JOIN station_details ON station_details.post_code = postcodelatlng.postcode
INNER JOIN core ON station_details.station_id = core.entity_id
WHERE core.live = 1
ORDER BY distance ASC";
$query .= ' limit ?, ?';
//echo $query;
if ($stmt = $conn->prepare($query)) {
$stmt->bind_param('sssii', $lattitude, $lattitude, $longitude, $limit_start, $limit_end);
$stmt->execute();
$stmt->bind_result($station_id, $distance);
while ($stmt->fetch()) {
echo "id: $station_id dist: $distance";
}
$stmt->close();
}
$conn->close();
?>