0

I'm creating an app that gets some values from a mysql database via php. I've gone as far as returning a string that's echoed via php and using it in objective C.

Here's what I have so far:

    NSString * strURL = [NSString stringWithFormat:@"http://localhost/search.php?name=%@",name];
    NSData * dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];        
    NSString * result = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
    NSLog(@"%@", result);

Is it possible to return 2 different strings from php and using them separately in xcode or do I have to make 2 different calls to the php file?

Thank you very much for your help!

Shak
  • 73
  • 6

1 Answers1

1

Great start!

Consider using some structured way to return data from PHP. One easy format that you can learn, which will help with other API integration later, would be JSON.

Apple ships some simple code to to the conversion in iOS5 with NSJSONSerialization

On the PHP side, play around with json_encode. You can pass in an indexed array, for example, which will give you an NSArray on the iOS side.

Some more examples for the iOS side: How to use NSJSONSerialization

Community
  • 1
  • 1
Adam B
  • 3,775
  • 3
  • 32
  • 42
  • Yep, that seems exactly what I'm looking for. I'll be back with the results :). Thank you for the fast reply! – Shak Aug 09 '12 at 00:56
  • Here is a [great tutorial on using JSON in iOS 5](http://www.raywenderlich.com/5492/working-with-json-in-ios-5). – Peter Gluck Aug 09 '12 at 01:28
  • Turns out JSON is the way to go for what I was trying. Thank you both for the great help. – Shak Aug 10 '12 at 21:56