views:

686

answers:

3

In php I have:

$result = mysql_query($query);
// if successful query, return the records
if ($result)
{
 // if not empty result, return array of records
 if (mysql_num_rows($result))
 {
  $records = array();
  while ($row = mysql_fetch_assoc($result))
  {
   $records[] = $row;
  }
  return $records;
 }
     }

In Objective-C - After the POST I have the following code...

NSURLResponse *newStr = [[NSURLResponse alloc] init];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&newStr error:nil];

returnData is equal to the literal "Array", not the contents of $records[]

A: 

On the PHP side you need to serialize the array to a format like JSON or XML rather than just printing it. On the iPhone side would then deserialize the NSData object using the appropriate library

rpetrich
Thanks for the feedback. I'm using php version 4.4.9, which doesn't support JSON, so I used XML instead. Thanks for the Answer.
Jordan
+2  A: 

You need to format your array in some format that you can read in Objective-C. I recommend using JSON.

If you have PHP 5.2, you can use the built in functions to encode your data. For example:

echo json_encode($records);

On the Objective-C side, you can use TouchJSON to decode/encode JSON.

NilObject
+1 for JSON. It's so easy it's criminal.
thomasrutter
I'm stuck with php version 4.4.9 unfortunately
Jordan
+1  A: 

You should serialise the data into a XML type plist format if you can, then it can be natively interpreted by Cocoa classes like NSArray and NSDictionary. NSPropertyListSerializer is capable of producing mutable or immutable arrays and dictionaries from a file or from an NSData object containing serialized data in plist format.

NSDictionary can also read files in this format:

"key" = "value";
"key2" = "value2";

Likewise, NSArray can read files in this format:

(
     "value1",
     "value2",
     "value3",
     "30",
     "value5"
);

In fact, I think if the strings don't contain spaces, the quotes are optional (but I guess they would force string types).

Check out this URL for more information about oldschool ASCII property lists. They are read-only, but then again they are easy enough to generate manually or systematically.

dreamlax
All good responses. Dreamlax, Good Idea. That makes a lot more sense to me now, especially since I'm not familiar with JSON. Thanks!
Jordan