【发布时间】:2013-11-30 17:32:41
【问题描述】:
我有一个 tableViewController,它显示按名称排序的地点列表!此列表是使用查询从我的 MySQL 数据库中检索到的
从城堡中选择 IDUser 按名称排序
但我的目标是根据 userLocation 按位置对它们进行排序。因此,数据库中的任何地方都有“纬度”和“经度”字段。
在我的 tableViewController 我这样做:
NSString *IDString = @"SELECT ID FROM Castle ORDER BY Name ASC";
NSMutableString *strURL = [NSMutableString stringWithFormat:@"http://localhost/myApp/fieldsRequest.php?query=%@",query];
[strURL setString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:dataURL
options:kNilOptions
error:&error];
NSMutableArray *results = [[NSMutableArray alloc] init];
int numRow = 0;
for (NSArray *arrow in json) {
[results addObjectsFromArray:arrow];
numRow++;
}
return results;
被调用的脚本 php:
<?php
require 'mycredentials.php';
$resultCredentials = sdbmyCredentials();
if ($resultCredentials == YES) {
$arr = array();
$query = $_GET[query];
$results = mysql_query($query) or die ("Unhandled exception: " . mysql_error());
// Add the rows to the array
while($obj = mysql_fetch_row($results)) {
$arr[] = $obj;
}
echo json_encode($arr);
}
?>
它运行良好,我按名称检索地点列表。 相反,当我这样做时:
_IDString = [NSString stringWithFormat:@"SELECT ID FROM Castle ORDER BY (POW((longitude-%f),2) + POW((latitude-%f),2))", longitude,latitude];
NSMutableString *strURL = [NSMutableString stringWithFormat:@"http://localhost/myApp/fieldsRequest.php?query=%@",query];
[strURL setString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:dataURL
options:kNilOptions
error:&error];
NSMutableArray *results = [[NSMutableArray alloc] init];
int numRow = 0;
for (NSArray *arrow in json) {
[results addObjectsFromArray:arrow];
numRow++;
}
return results;
我在字符串中作为参数给出的纬度和经度分别是 40.535568 和 16.503588 如果我在 phpMyAdmin 中运行 sql 命令:
从 Castle ORDER BY 中选择 ID (POW((longitude-16.503588),2)+POW((latitude-40.535568),2))
它工作得很好,它给了我结果。但是如果我按照我之前写的代码执行它,它不会产生结果,而是会出错:
未处理的异常:您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 在第 1 行的 'POW((latitude-40.535568),2))' 附近使用的语法
这是字符串地址:
http://localhost/myApp/fieldsRequest.php?query=SELECT%20ID%20FROM%20Castle%20ORDER%20BY%20(POW((longitude-16.503588),2)+POW((latitude-40.535568),2))
【问题讨论】:
-
不相关,但在您的 PHP 中,您可能需要考虑使用 mysqli 或 PDO。见PHP: Choosing a MySQL API。
-
您的
POW错误,我面前没有电脑,但我在SQLite Core Functions 列表中看不到该功能。我会尝试将其简化为基本的算术表达式。
标签: php mysql ios objective-c json