2015-05-29 105 views
1

为什么这个查询不起作用?子查询计算错误?

$query = ("SELECT * FROM 
       (SELECT *, (
       (((endingLatitude - " . $h . ")(endingLatitude - " . $h . ")) 
       /" . pow($r1, 2) . ") 
       + 
       ((endingLongitude - " . $k . ")(endingLongitude - " . $k . ")) 
       /" . pow($r2, 2) . " 
       ) AS point2 
       FROM (SELECT *, ((
       (
       ((startingLatitude - " . $h . ")(startingLatitude - " . $h . ")) 
       /" . pow($r1, 2) . ") 
       + 
       ((startingLongitude - " . $k . ")(startingLongitude - " . $k . ")) 
       /" . pow($r2, 2) . ")) 
       AS point1 
       FROM (SELECT * FROM trips WHERE distance >='" . ($distance * .25) . "') as query1) 
       as query2) as query3 WHERE point1 <= 1 AND point2 <= 1 LIMIT 0 , 10;"); 

$result = mysqli_query($con, $query); 

$ h和$ k分别是椭圆的x和y坐标。我使用公式here来计算两点(startingLat,startingLong)和(endingLat,endingLong)是否位于垂直高度$ r1和水平高度$ r2的椭圆内。我还将我搜索的行限制为距离单元格值大于$ distance * .25的行。

我认为这可能与括号错误有关,或与我子查询/执行计算的方式有关。

使用

die(mysqli_error($con)); 

返回You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(endingLatitude - 36.9564753)) /796.842964388) + ((endingLongitud' at line 3

+0

你做了什么错误? –

+0

@anantkumarsingh失败'if(!$ result)'然后打印,'$ result = mysqli_query($ con,$ query);' –

+0

把'$ result = mysqli_query($ con,$ query)或者死(mysqli_error($ con));'并检查你所得到的真实错误。谢谢。 –

回答

2

相信你已经使用MySQL乘法算术运算符,*错误。 https://dev.mysql.com/doc/refman/5.0/en/arithmetic-functions.html#operator_times

相反的:

(endingLatitude - " . $h . ")(endingLatitude - " . $h . ") 

操作方法......

(endingLatitude - " . $h . ") * (endingLatitude - " . $h . ") 
+0

这可能是它!解决了错误,但是,我的查询仍然无法正常工作,所以它一定是别的......我会研究它。 –