简体   繁体   English

为什么我无法使用PHP / MySQL和纬度/经度从此查询中获取数组?

[英]Why am I not able to get an array from this query using PHP / MySQL and latitude / longitude?

I have two tables but for the purposes of this question, I will only use one. 我有两个表,但是出于这个问题的目的,我只会使用一个表。 I am using the latitude and longitude for a city to find airports within 50 miles. 我正在使用城市的经度和纬度来查找50英里内的机场。 As far as I know, it's working but I can't seem to put this into an array with mysql_fetch_array... It's something stupid or something small... it always seems to be one or the other. 据我所知,它正在工作,但是我似乎无法使用mysql_fetch_array将其放入一个数组中……这有点愚蠢或很小……它似乎总是一个或另一个。 After trying several things, this is where I am at right now with the code: 在尝试了几件事之后,这就是我现在使用代码的位置:

<?php 
  require('dbconnect.php');
  //airports Table Columns
    //iata_code
    //airport_name
    //airport_name_clean
    //city_id
    //airport_lat
    //airport_long

$cityLat = "25.788969"; //Miami
$cityLong = "-80.226439"; //Miami
$distance = "10"; //miles?

$airportQuery = mysql_query("select airport_name, 
   ( 3959 * acos( cos( radians($cityLat) ) 
          * cos( radians( locations.lat ) ) 
          * cos( radians( locations.lng ) - radians($cityLong) ) 
          + sin( radians($cityLat) ) 
          * sin( radians( locations.lat ) ) ) ) AS distance 
from airports 
and locations.lat between X1 and X2 
and locations.Long between y1 and y2
having distance < $distance ORDER BY distance;
");

while($airports = mysql_fetch_array($airportQuery))
    {
        echo $airports['airport_name'] . "<br />";
    }

?>

As always, any help will be greatly appreciated. 一如既往,任何帮助将不胜感激。 Thank you so much for your help! 非常感谢你的帮助!

Your query looks suspicious, I think you have an and when you should have a where : 您的查询看起来很可疑,我想您有一个and何时应有where

$airportQuery = mysql_query("select airport_name, 
   ( 3959 * acos( cos( radians($cityLat) ) 
          * cos( radians( locations.lat ) ) 
          * cos( radians( locations.lng ) - radians($cityLong) ) 
          + sin( radians($cityLat) ) 
          * sin( radians( locations.lat ) ) ) ) AS distance 
from airports 
WHERE locations.lat between X1 and X2 
and locations.Long between y1 and y2
having distance < $distance ORDER BY distance;
");

You should check the return value for errors, that might be informative as well. 您应该检查返回值是否有错误,这可能也有参考价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM