我想感謝過去和未來的所有人,他們幫助我完成了我的旅程。所以,我有一個iOS的新應用程序,我想從iOS應用程序搜索MYSQLi數據庫。從那裏,我希望它在UITableView中填充結果。我已經有了表格視圖的代碼。我也已經知道如何在PHP中使用json_encode函數。我的問題是,如何從iOS應用程序向MYSQL數據庫發送查詢並將其作爲JSON返回?例如,下面是我如何從我的網站搜索我的MYSQL數據庫。我想從我的iOS應用程序中做同樣的事情。iOS應用程序中的搜索欄搜索PHP/MYSQLi並將JSON解析回iOS應用程序?
我應該提到,我已經使用JSONSerialization類,並且解析是很好的。我只是不知道如何從iOS發送查詢到MYSQL並返回。
<?php
$search = $_POST['searchquery'];
$query = "SELECT * FROM blacklisted WHERE MATCH (name, address, city, state, zip) AGAINST ('%$search%' IN BOOLEAN MODE)";
$result = mysqli_query($connection, $query);
$numrows = mysqli_num_rows($result);
if ($search == null)
{
echo "Search The Blacklist Today!";
}
else if($search != null)
{
while ($row = mysqli_fetch_array($result))
{
$name = $row['name'];
$address = $row['address'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$date = $row['date'];
$persons = $row['persons'];
$damages = $row['damages'];
$complaint = $row['complaint'];
?>
#import "Search.h"
@implementation Search
NSDictionary *dictionary;
NSArray *complaints;
NSString * json;
NSData * data;
NSMutableString* mainlabel;
NSMutableString* detaillabel;
NSMutableURLRequest *request;
@synthesize searchfield, table;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSString*searchtext = searchfield.text;
NSString *post =[[NSString alloc] initWithFormat:@"%@", searchtext];
NSURL *url=[NSURL URLWithString:@"http://www.zebradatasolutions.com/theblacklist/jsonsearch.php"];
NSData *postData = [post dataUsingEncoding: NSASCIIStringEncoding allowLossyConversion: YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue: postLength forHTTPHeaderField:@"Content-Length"];
[request setValue: @"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest: request returningResponse: & response error: & error];
json =[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
data =[json dataUsingEncoding:NSUTF8StringEncoding];
dictionary = [NSJSONSerialization JSONObjectWithData: data options:NSJSONReadingAllowFragments error: nil];
complaints = [dictionary valueForKey:@"blacklistsearch"];
[table reloadData];
NSLog(@"dictionary = %@", dictionary);
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}
NSString * name = complaints[indexPath.row][@"name"];
NSString *address = complaints[indexPath.row][@"address"];
NSString *city = complaints[indexPath.row][@"city"];
NSString *state = complaints[indexPath.row][@"state"];
NSString *zip = complaints[indexPath.row][@"zip"];
NSString *date = complaints[indexPath.row][@"date"];
NSString *persons = complaints[indexPath.row][@"persons"];
NSString *damages = complaints[indexPath.row][@"damages"];
NSString *complaint = complaints[indexPath.row][@"complaint"];
mainlabel = [[NSMutableString alloc]init];
detaillabel = [[NSMutableString alloc]init];
[mainlabel appendString:[NSMutableString stringWithFormat:@"%@", name]];
[detaillabel appendString: [NSMutableString stringWithFormat:@"\n\n%@\n%@, %@\n%@\n\n%@\n\n%@\n\n%@\n\n%@\n", address, city, state, zip, date, persons, damages, complaint]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = mainlabel;
cell.detailTextLabel.text = detaillabel;
cell.textLabel.textColor = [UIColor magentaColor];
cell.detailTextLabel.textColor = [UIColor whiteColor];
return cell;
}
-(IBAction) closekeyboard: (id)sender
{
[searchfield resignFirstResponder];
}
@end
謝謝盧卡...這是正確的答案,但我仍然有點困惑。所以MYSQL正在監聽一個$ _POST ['searchtext'],我正在嘗試從iOS提供它。你可以看到我的搜索引擎在瀏覽器中工作...所以如果有許多用戶使用這個應用程序,我會不使用PHP端的會話創建問題? – Julian
因此,服務器檢索所有參數請求(所以POST和GET,可以使用$ _POST ['param_key']或$ _GET ['param_key'],使用PHP檢索),而MySql是用於存儲和檢索所有數據庫的DB你的信息。一旦你有客戶端參數(在我的例子中是與'myValue'鍵相關的值),你建立字符串查詢然後調用返回查詢結果的PHP方法'mysqli_query'(如數組或對象結構),如果沒有錯誤occours。 關於會話使用,我建議你避免它,如果你需要在服務器數據庫上執行一個簡單的搜索查詢。反正沒有問題 –