2013-12-18 151 views
1

我正在爲iOS應用程序編寫一個API,並且需要在地理座標附近獲取位置。MySQL查詢不在MySQLi中工作,但在MySQL中工作

我在mysqli中試過這個查詢,它沒有返回任何結果。當我在一個常規的mysql_query中完成時,它完美地工作。我也在phpmyadmin中測試了它的成功完成的查詢。

SELECT id, name, address, city, state, longitude, latitude, ($miles * acos(cos(radians($latitude)) * cos(radians(latitude)) * cos(radians(longitude) - radians($longitude)) + sin(radians($latitude)) * sin(radians(latitude)))) AS distance 
     FROM locations HAVING distance < $distance ORDER BY distance LIMIT 0, 20 

這與變量查詢填寫:

SELECT id, name, address, city, state, longitude, latitude, (3959 * acos(cos(radians(40.735767)) * cos(radians(latitude)) * cos(radians(longitude) - radians(-73.991806)) + sin(radians(40.735767)) * sin(radians(latitude)))) AS distance 
     FROM locations HAVING distance < 25 ORDER BY distance LIMIT 0, 20 

有我丟失的東西,爲什麼它不會在mysqli工作?

這是工作MySQL查詢代碼

mysql_connect('localhost', 'test', 'test') or die(mysql_error()); 
mysql_select_db('testdb') or die(mysql_error()); 
$locations = array(); 
$miles = 3959; 
$distance = 25; 
$latitude = "40.735767"; 
$longitude = "-73.991806"; 

$data = mysql_query("SELECT id, name, address, city, state, longitude, latitude, ($miles * acos(cos(radians($latitude)) * cos(radians(latitude)) * cos(radians(longitude) - radians($longitude)) + sin(radians($latitude)) * sin(radians(latitude)))) AS distance 
     FROM locations HAVING distance < $distance ORDER BY distance LIMIT 0, 20") 
    or die(mysql_error()); 

while ($row = mysql_fetch_assoc($data)) { 
    array_push($locations, $row); 
} 

我使用MysqliDb類https://github.com/ajillion/PHP-MySQLi-Database-Class所以應該工作如下

 $row = $db->rawQuery("SELECT id, name, address, city, state, longitude, latitude, (? * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))) AS distance 
     FROM locations HAVING distance < ? ORDER BY distance LIMIT 0, 20", array($miles, $latitude, $longitude, $latitude, $distance)); 

     if (count($row) > 0){ 
      // if found, return JSON response 
      echo json_encode($row[0]); 
     } 

即使當我用我發現mysqli的基本模板,它失敗。

$locations = array(); 
$miles = 3959; 
$distance = 25; 
$latitude = "40.735767"; 
$longitude = "-73.991806"; 

// Connect to database 
$link = mysqli_connect('localhost','test','test','testdb'); 

// Check for Errors 
if(mysqli_connect_errno()){ 
    echo mysqli_connect_error(); 
} 

// Prepare Query 
$query = "SELECT id, name, address, city, state, longitude, latitude, ($miles * acos(cos(radians($latitude)) * cos(radians(latitude)) * cos(radians(longitude) - radians($longitude)) + sin(radians($latitude)) * sin(radians(latitude)))) AS distance 
     FROM locations HAVING distance < $distance ORDER BY distance LIMIT 0, 20"; 

// Escape Query 
$query = mysqli_real_escape_string($link,$query); 

// Perform Query 
if($result = mysqli_query($link,$query)){ 

// Cycle through results 
while($row = mysqli_fetch_object($result)){ 
    array_push($locations, $row); 
} 
// Free Result Set 
mysqli_free_result($result); 
} 

// Close Connection 
mysqli_close($link); 
+4

哪裏調用mysql和mysqli函數? – AbraCadaver

+0

這個例子沒有使'mysql'或'mysqli'調用的代碼是沒有用的。意思是,這些查詢可能100%沒有錯,但也許你的PHP實現是關閉的。 – JakeGould

+0

取決於您如何查詢它。您只向我們展示一行代碼。向我們展示您的完整代碼。 –

回答

1

有我丟失的東西,爲什麼它不會在mysqli的工作嗎?


MySQL和mysqli的API的運行查詢完全相同的方式。

尋找錯別字。和其他類型的錯誤。

相關問題