我在我的數據庫中有很多優惠。我想通過發送參數「緯度」,「經度」和半徑。要seleect從數據庫中的一些特定的交易 此查詢心不是工作是什麼wront它..選擇特定距離內的點
require('includes/connection.php'); // defines $dsn, $username, $password
$lat = $_GET['lat']; // latitude of centre of bounding circle in degrees
$lon = $_GET['lon']; // longitude of centre of bounding circle in degrees
$rad = $_GET['rad']; // radius of bounding circle in kilometers
$R = 6371; // earth's radius, km
// first-cut bounding box (in degrees)
$maxLat = $lat + rad2deg($rad/$R);
$minLat = $lat - rad2deg($rad/$R);
// compensate for degrees longitude getting smaller with increasing latitude
$maxLon = $lon + rad2deg($rad/$R/cos(deg2rad($lat)));
$minLon = $lon - rad2deg($rad/$R/cos(deg2rad($lat)));
// convert origin of filter circle to radians
$lat = deg2rad($lat);
$lon = deg2rad($lon);
$sql = " Select * From From deals
Where lat>$minLat And lat<$maxLat
And lon>$minLon And lon<$maxLon
Where acos(sin($lat)*sin(radians(lat)) + cos($lat)*cos(radians(lat))*cos(radians(lon)-$lon))*$R < $rad";
echo "Query =".$sql;
$points = mysql_query($sql);
你是什麼意思的「不工作」。它沒有提供任何結果,它是否給出錯誤消息...?請更具體一點。你可以馬上嘗試打印sql查詢字符串並通過例如執行結果語句。 phpMyAdmin的? – codeling 2012-01-03 14:28:37
你不能只在查詢中用$來傳遞變量,你必須使用sprintf,連接或使用類似的解決方案。請參閱http://php.net/manual/en/function.mysql-query.php – Viruzzo 2012-01-03 14:30:39
@Viruzzo中的示例:實際上應該按照我所知的方式工作(請參閱http://www.brainbell.com/tutors /php/php_mysql/Variable_substitution.html) – codeling 2012-01-03 14:32:30