I followed Ray Wenderlich's tutorial on creating a web service / hooking it up to iOS but his example the returned results only have one possible row returned in the query. I'd like to return all possible results in a JSON format but I am confused how to store them as the correct key.
Here's what I have for the PHP:
$stmt = $this->db->prepare('SELECT userID, lat, lng FROM Connections WHERE airline=? AND flight_number=? ');
$stmt->bind_param("si", $airline, $flight_number);
$stmt->execute();
$stmt->bind_result($otherUser, $otherLat, $otherLng);
while ($stmt->fetch()) {
break;
}
$stmt->close();
if ($otherUser) {
//sendResponse(403, 'Code already used');
//return false;
$myLatLng = "$lat,$long";
$otherLatLng="$otherLat,$otherLng";
$results = getDistanceBetween($myLatLng,$otherLatLng);
$miles = $results[0];
$minutes = $results[1];
$result = array(
"other_user" => $otherUser,
"distance" => $miles,
"duration" => $minutes,
);
sendResponse(200, json_encode($result));
return true;
}
On the Obj-C side of things I get these values using this code:
if (request.responseStatusCode == 200) {
NSString *responseString = [request responseString];
NSDictionary *responseDict = [responseString JSONValue];
NSString *otherUser = [responseDict objectForKey:@"other_user"];
NSString *otherDistance = [responseDict objectForKey:@"distance"];
NSString *otherDuration = [responseDict objectForKey:@"duration"];
Can someone please help me out?