views:

80

answers:

2

I want to connect to a mySQL database from my iPhone application.

How should I do this?

A: 

I am assuming you are using PHP?

If you are. Use JSON, I have used PLISTS as well but JSON is soo simple.

  1. In PHP you dont need to do anything eg

    $mydata = array("error"=>false, "msg"=>"this is cool"); echo json_encode($mydata);

wow that was pretty simple, you can work with native objects in PHP and to send them to the phone you put them in one big object. You can have nested objects as well.

  1. On the iPhone you do need get some JSON source code. Download this JSON framework http://code.google.com/p/json-framework/

  2. You should have installed, or dragged the JSON folder into you Xcode project.

  3. In data class you load the data from PHP using a normal request.

    import "JSON.h"

then once you have download the JSON data from you website (as text).

SBJSON *jsonParser = [SBJSON new];
// Parse the JSON into an Object
NSError *error;
id  resultsData =  [jsonParser objectWithString:YOUR_DATA_STRING error:&error];
if ([resultsData objectForKey:@"error"] != nil) {
    NSLog(@"error");
}else{
   NSLog(@"the msg is %@",[resultsData objectForKey:@"msg"] );
}

You can iterate over objects etc to get nested data.

This is the easiest and most flexible way to get data from PHP into you iPhone app.

I would also recommending using a HTTP proxy tool like charles http://www.charlesproxy.com/ This way you can actually see the json object coming down the pipes and view the data in a nice try like structure.

John Ballinger
Thanks for reply... I have no idea how to use jason can you send me some good article links which will give me help to use Jason....Thank you so much...
Nauman.Khattak
google it. Come on dont be lazy. 1. Get PHP working. I have given you a great example at formatting some data. Use charles to see that data coming back from the website. 2. I have also shown you how to read JSON in your code. 3. If you can just get the text showing up in your code from your PHP you would be away.@satyam says you can use csv but if you have a comma in your content you will still need to implement a CSV parser to escape commas.
John Ballinger
A: 

You can create some webservices on your server using PHP or ASP or some other service. You don't need to have specifically JSON or XML....... It can be as simple as comma separated value (CSV). It all depends on how your app understands the output of the webservices.

Satyam svv