I'm attempting to select users from a database (displaying them in a tableview) using a value that is retrieved from a variable.
Here is the code that accomplishes it:
NSString *post = [[NSString alloc] initWithFormat:@"location=%@", _detailText];
NSLog(@"detailtextCount=%@", _detailText);
NSURL * url = [NSURL URLWithString:getDataURL];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long) [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response=nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&error];
if ([response statusCode] >=200 && [response statusCode]<300) {
}
NSData * data = [NSData dataWithContentsOfURL:url];
_jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
_usersArray = [[NSMutableArray alloc] init];
for (int i=0; i < _jsonArray.count; i++) {
NSString * uName = [[_jsonArray objectAtIndex:i] objectForKey:@"Username"];
[_usersArray addObject:[[Locations alloc] initWithUserName:uName]];
NSLog(@"useresArray=%@", uName);
[self.tableViews reloadData];
}
My PHP script after connecting to db:
$location = $_POST['location'];
$query = "SELECT * FROM user WHERE location= '$location'";
$resultset = mysql_query($query,$connection);
$records = array();
while($r = mysql_fetch_assoc($resultset))
{
$records[] = $r;
}
echo json_encode($records);
}
My NSLog
for _detailText
returns the correct value, thought my tableView is left empty. When i copy the contents of _detailText
and place them in my PHP script in place of $location
it will correctly populate my tableView, so somewhere the variable $location
is getting lost.