4

I am trying to create a web service that serves up json from a mysql database through php for display in an iPhone app.

I have a standard mysql/php setup.

The data is in a table with fields and records. It is queried with sql to create a record set. Each record in the recordset is a row.

php

$sql = "SELECT userid,task,longtask FROM tasks WHERE userid = 1 LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());  
$tasks = array();
while($row = mysql_fetch_assoc($res)) {
$tasks[] = array('row'=>$row);
 } 
echo json_encode(array('tasks'=>$tasks));
//

The web service produces the following output:

{"tasks":[{"row":{"userid":"1","task":"send email to Bob","longtask":"include attached memo"}}]}

However, I'm having a lot of trouble getting this to read into IOS suggesting that there might be a better format for the web service.

The structure is different from that in tutorials and other sources I have found for reading the json into IOS (none of which use php/mysql).

Can anyone tell me a better way to structure the json or alternatively code to read this json in iOS to grab the userid, task and other variables.

When I try this, I get an error that it cannot rad row at index:0

      NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData //1
                                                         options:kNilOptions 
                                                           error:&error];
    NSLog(@"about to print json: %@",json);
NSMutableArray *getElement = [json objectForKey:@"tasks"];
    for (NSDictionary *dict in getElement) {
        NSArray *array = [dict objectForKey:@"row"];
        NSString *str = [array objectAtIndex:0];
    }

Thanks in advance for any suggestions.

2
  • Have you tried laravel.com framework? Commented Jul 5, 2015 at 22:45
  • Trying to avoid frameworks until I really understand what is going on. Am finding the json format i.e. what is a dictionary and what is an array and whether you can control this in server output confusing. Commented Jul 6, 2015 at 19:42

1 Answer 1

1

Method 1: Edit PHP Code

Index 0 is not available because you are fetching an associative array from PHP using mysql_fetch_assoc.

Using mysql_fetch_array will return an array containing both zero-based indices and associative keys by default.

$sql = "SELECT userid,task,longtask FROM tasks WHERE userid = 1 LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());  
$tasks = array();
while($row = mysql_fetch_array($res)) {
    $tasks[] = array('row'=>$row);
} 
echo json_encode(array('tasks'=>$tasks));

Will output

{"tasks":[{"row":{"0":"1","userid":"1","1":"send email to Bob","task":"send email to Bob","2":"include attached memo","longtask":"include attached memo"}}]}

Now you can retrieve the userid using either of the keys 0 or userid.

Method 2: Edit iOS Code

Edit iOS Code. But for this to work, you will have to know the keys in the row.

NSString *str = [array objectForKey:@"userid"];`
Sign up to request clarification or add additional context in comments.

2 Comments

The first method of changing to mysql_fetch_array seems like it should work but it still generates same error. Where would you put edit to iOS code? In place of NSString it generates Expected expression and no visible @interface for NSArray declares the selector objectforkey errors.
I am guessing since both types of arrays have been combined, the 0 is also considered as a key and not an index. This makes the array to be an object. Try mysql_fetch_array($res, MYSQL_NUM); and this would return you only an index-based array. If this is done, I don't think you might need the iOS edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.