![](/img/trans.png)
[英]PhpMyAdmin running stored procedures works through interface, but running as a query doesn't work
[英]Query works through phpMyAdmin but doesn't work through php script called from viewController
我有一個tableViewController,它提供了按名稱排序的地方列表! 使用查詢從我的MySQL數據庫檢索此列表
SELECT IDUser FROM Castle ORDER BY Name
但我的目標是根據用戶位置按位置對它們進行排序。 因此,數據庫中的任何位置都具有“緯度”和“經度”字段。
在我的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;
如果我運行sql命令,則在字符串中作為參數給出的緯度和經度分別是40.535568和16.503588。
從Castle ORDER BY中選擇ID(POW((經度16.503588),2)+ POW((緯度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))
問題在於stringByAddingPercentEscapesUsingEncoding
僅轉義那些通常在URL中無效的字符。 但是一般來說,URL中有一些允許的字符(例如&
和+
),但在URL參數中沒有問題(例如&
分隔參數, +
轉換為空格字符)。
因此,您希望使用一種方法,不僅可以對URL中無效的那些字符進行百分比轉義,而且還可以對URL中通常具有特殊含義但在參數中存在問題的那些字符進行轉義,而不是stringByAddingPercentEscapesUsingEncoding
。 在這種情況下,您希望對SQL中出現的+
進行百分比轉義。
最簡單的方法是使用CFURLCreateStringByAddingPercentEscapes
,而不是stringByAddingPercentEscapesUsingEncoding
:
- (NSString *)percentEscapeURLParameter:(NSString *)string
{
return CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)string,
NULL,
(CFStringRef)@":/?@!$&'()*+,;=",
kCFStringEncodingUTF8));
}
而且,順便說一句,您應該將參數轉義到URL,而不是URL本身。
NSString *sql = [NSString stringWithFormat:@"SELECT ID FROM Castle ORDER BY (POW((longitude-%f),2) + POW((latitude-%f),2))", longitude, latitude];
NSString *percentEscapedSQL = [self percentEscapeURLParameter:sql];
NSString *strURL = [NSString stringWithFormat:@"http://localhost/myApp/fieldsRequest.php?query=%@", percentEscapedSQL];
// don't percent-escape the full URL
//
// [strURL setString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
順便說一句,我衷心勸阻您不要在URL中傳遞SQL字符串。 這是一個嚴重的安全風險。 您應該只傳遞參數(例如,緯度和經度),然后讓PHP驗證這些參數,然后構建SQL本身。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.