views:

764

answers:

2

I have a server-side php-script that fetches results from a MySQL table. I am trying to retrieve these results from within an iPhone OS app. I am using the following method:

NSURL *myURL = [NSURL URLWithString:@"http://www.someurl.com/login.php?id=anid"];
NSArray *sqlResults = [[NSArray alloc] initWithContentsOfURL:myURL];

//Show me what went into the Array
NSLog(@"%@", [sqlResults ObjectAtIndex:0]);

*The NSLog at the bottom is me trying to see what just transpired. The NSLog statement is printing out (null) though -- so I am stuck. My problem is that I don't know what the heck I am trying to do with the NSArray of results and I am probably doing many things wrong. Is an internet connection implied (Have Cellular and Wi-fi)? So, my question is: What should I be doing to get MySQL query-results using -initWithContentsOfURL?

+1  A: 

According to the documentation on -[NSArray initWithContentsOfURL:] say;

The array representation at the location identified by aURL must contain only property list objects (NSString, NSData, NSArray, or NSDictionary objects).

To my understanding, that means that it must be in plist format. If your results aren't in plist format, then try using -[NSString initWithContentsOfURL:] instead. If you then want to split it based on lines, you could call [downloadedString componentsSeparatedByString:@"\n"]

BJ Homer
+2  A: 

In which format is your PHP script returning the MySQL results? It looks as though [NSArray initWithContentsOfURL:] requires the data to be in a very specific 'property list' format. Have you tried to see what output you get if you use NSString instead?:

[NSString initWithContentsOfURL:encoding:error:]

This should at least show you what you are getting out of your PHP script. Finally, take a take at the property list format. You may need to perform a transformation in your server side script or parse your PHP result set format on the iPhone as BJ Homer suggested.

teabot
Tried it and it gave me just what I was looking for. -Thanks
RexOnRoids