我正在为iOS应用程序整理API,并且需要在地理坐标附近获取位置。
我在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 Class 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);
答案 0 :(得分:1)
有什么我不知道为什么它在mysqli中不起作用?
没有。
mysql和mysqli API都以完全相同的方式运行您的查询。
寻找拼写错误。和其他类似的错误。