doufubian3479 2012-05-21 09:28
浏览 20
已采纳

查询在php文件中失败,但从不在phpmyadmin中

$query = "SELECT *, ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon - lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance 
            FROM average_temp 
            HAVING distance<=25 
            ORDER BY distance ASC limit 1";

I can run the above query without any problems in phpmyadmin but it fails about 20% of the time if I run it through my php file.

This is the code that deals with the query results:

if (!$query){
  die ("Query failed: " . mysql_error());
}
$result = mysqli_query($db, $query);
$num_results = mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);
$var = $row['id'];

80% of the time it works great and the other 20 it hangs for 30sec and throws this error:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\w

What I'm doing is taking latitude and longitude values from another table and referencing them with this table. The formula works like a charm on finding locations within a given distance. I have it set at 25 miles and only want the closest match but you could change it to give you the nearest # of locations. I found the query here.

Any ideas what I could be doing wrong?

  • 写回答

2条回答 默认 最新

  • doumi1912 2012-05-21 09:33
    关注

    First off, you're not testing the result of the query but the query string itself. It should be something like:

    $result = mysqli_query($db, $query);
    if ($result === false){
        die ("Query failed: " . mysql_error());
    }
    $num_results = mysqli_num_rows($result);
    $row = mysqli_fetch_assoc($result);
    $var = $row['id'];
    

    That way you should get proper error reporting from MySQL, I'm guessing a timeout since it hangs for 30 seconds.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?