2015-05-29 134 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

這可能是它!解決了錯誤,但是,我的查詢仍然無法正常工作,所以它一定是別的......我會研究它。 –