[英]Search Bar in iOS App to search PHP/MYSQLi and parse JSON back to iOS app?
I want to thank everyone here from the past and in the future who have helped me on my journey. 我要感谢这里过去和将来对我的旅程有所帮助的每个人。 So, I have a new app for iOS where I want to search a MYSQLi database from the iOS app.
因此,我有一个适用于iOS的新应用,我想从iOS应用中搜索MYSQLi数据库。 From there, I want it to populate the results in a UITableView.
从那里,我希望它在UITableView中填充结果。 I already have the code for the Table View.
我已经有了表视图的代码。 I also already know how to use the json_encode function in PHP.
我也已经知道如何在PHP中使用json_encode函数。 My question is how do you send a query to a MYSQL database from an iOS app and have it come back as JSON?
我的问题是如何将查询从iOS应用发送到MYSQL数据库并以JSON形式返回? For example, below is how I search my MYSQL database from my website.
例如,下面是如何从我的网站搜索MYSQL数据库的方法。 I want to do the same thing from inside my iOS app.
我想在iOS应用程序中执行相同的操作。
I should mention, I am already using JSONSerialization class and the parsing is good. 我应该提到,我已经在使用JSONSerialization类,并且解析很好。 I just don't know how to send queries from iOS to MYSQL and get something back.
我只是不知道如何将查询从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'];
?>
The Blacklist JSON Output 黑名单JSON输出
#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
You wrong to set the search parameter in post data. 您错误地在帖子数据中设置了搜索参数。
instead of 代替
NSString *post =[[NSString alloc] initWithFormat:@"%@", searchtext];
you should do something like 你应该做类似的事情
NSString *post =[[NSString alloc] initWithFormat:@"myValue=%@&otherValue=%@", searchtext,otherTextYouWant];
where "myValue" and "otherValue" are the keys you'll use server side to retrieve the value searchtext and otherTextYouWant ( like $_POST['myValue']
) 其中“ myValue”和“ otherValue”是您将使用服务器端检索值searchtext和otherTextYouWant的键(例如
$_POST['myValue']
)
Moreover, you should encode all your param before send to post, maybe using this 此外,您应该在发送到发布之前对所有参数进行编码,也许使用此
-(NSString *)urlEncodeStrirng:(NSString*)baseString UsingEncoding:(NSStringEncoding)encoding {
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)baseString,NULL, (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", CFStringConvertNSStringEncodingToEncoding(encoding)));
}
so your row code will became 这样您的行代码将变为
NSString *post =[[NSString alloc] initWithFormat:@"myValue=%@", [self urlEncodeString:searchtext ] searchtext UsingEncoding:NSUTF8StringEncoding];
Remeber to escape received searchtext server side before query it to db, using addSlashes ( or changed way to query db, maybe using PDO ) to avoid sql-injection 使用addSlashes(或更改查询数据库的方式,也许使用PDO)来避免在查询到db之前对接收到的searchtext服务器端进行转义以避免sql-injection
I hope it could help you 希望对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.