【问题标题】:MySQL query only returning one resultMySQL 查询只返回一个结果
【发布时间】:2011-07-05 06:35:51
【问题描述】:

我有一个基于 Haversine 算法获取位置的查询。

SELECT
 id, description, name, 
 lat, `long`, 
 ( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( `long` ) - radians($long) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance 
FROM 
 places 
HAVING 
 distance < 10 
ORDER BY 
 distance 
LIMIT 0, 20;

然后我在 JSON 数组中回显它,如下所示:

$location = mysql_fetch_assoc($getlocations);
return print_r(json_encode($location));

但是,当应该至少有两行时,它只返回一行。任何人都知道它为什么会这样做?谢谢!

【问题讨论】:

  • 你为什么使用 HAVING 而不是 WHERE 而没有 GROUP BY。这个查询有效吗??
  • 你是否在while中使用mysql_fetch_assoc
  • @niktrs 是的,除了我提到的错误之外,它完美无缺。
  • 我认为因为mysql_fetch_assoc从第一个指针开始,要获得第二个左右,你需要迭代。
  • 当您有超过 1 行结果时,您必须使用循环来检索所有结果!

标签: php mysql


【解决方案1】:
while( $row = mysql_fetch_assoc($getlocations)){
    $location[] = $row;
}
return print_r(json_encode($location));

【讨论】:

    【解决方案2】:

    你可能需要在while循环中使用mysql_fetch_assoc()函数。

    i.e:
    while($location = mysql_fetch_assoc($getlocations));
    
    print_r($location);
    

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多