如何在mysqli_query [PHP]中包含数学?

时间:2018-06-07 15:54:56

标签: php mysql sql

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) 
* cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin(radians(lat)) ) ) AS distance 
FROM markers 
HAVING distance < 25 
ORDER BY distance 
LIMIT 0 , 20;

MySQL Great Circle Distance (Haversine formula)

我目前正在尝试在我的php代码中使用此查询,但我不确定如何在mysqli_query中成功实现它。

    $query = "SELECT FROM MyTable, (3959 * acos(cos(radians(37)) * cos(radians(TableLat)) * cos(radians(TableLng) - radians(-122)) + sin(radians(37)) * sin(radians(TableLat)))) 
    AS distance 
    HAVING distance < 25 
    ORDER BY distance 
    LIMIT 0 , 20";

    $stmt = mysqli_query($connectionInfo->conn, $query);

    if (!$stmt)
    {
        echo $connectionInfo->conn->error;
    }

这显然不起作用,因为所有与数学相关的东西都嵌套在“”中。在尝试创建查询时如何绕过此操作?

错误讯息:

  

您的SQL语法有错误;检查手册   对应于您的MariaDB服务器版本,以获得正确的语法   'FROM MyTable附近,(3959 * acos(cos(弧度(37))* cos(弧度(纬度))   * cos(第1行的'放射'

1 个答案:

答案 0 :(得分:0)

您的语法错误,FROM在字段之前:

$query = "SELECT (3959 * acos(cos(radians(37)) * cos(radians(TableLat)) * cos(radians(TableLng) - radians(-122)) + sin(radians(37)) * sin(radians(TableLat)))) 
AS distance 
FROM MyTable
HAVING distance < 25 
ORDER BY distance 
LIMIT 0 , 20";