-2

Possible Duplicate:
How to connect to a MySQL database from an iPhone?

I need to develop an Iphone Application where,i need to have a setting option ,which user can use to create profile and then login specific profile. I know that i need to have remote MySql server for this and have to use webservice for the same. As i am new to iphone development so can any body help me how to proceed with any example.

In simple words how do i connect MySql to Iphone app and make it possible to upload and fetch data from the MySql server.

Community
  • 1
  • 1
the monk
  • 389
  • 4
  • 14

2 Answers2

4

As stated above, the way to go about this is using GET/POST request scripts. I personally use PHP for these. For example, your script might look like this.

    $username = $_POST['username'];
    $password = $_POST['password'];

    $query = "SELECT * FROM users WHERE username='".$username."' AND password='".$password."';";
    $result = mysqli_query($db, $query);

    if ($row = mysqli_fetch_assoc($result)) {
         // This means a match was found in the DB
    }

I ususally setup my login scripts with much more security but this is the idea behind it.

It's easy to send a GET request from objective-c using NSURLConnection

    NSURLConnection *request = [NSURLConnection requestWithURL: @"http://www.website.com/script.php?username=user&password=pass"];

This will send a GET request to the specified URL. You would then have to change the $_POST[''] values to $_GET[''] in the PHP script.

Sending POST requests are a little more complicated in objective-c but follow a similar pattern.

I've recently started using ASIHTTPRequest to perform all of my GET/POST requests. I recommend you take a look at that. It's an open source library for this exact problem.

http://allseeing-i.com/ASIHTTPRequest/How-to-use

CorboKhan
  • 101
  • 3
1

The safest way to do this is to make an 'API' for your web service that fetches the data that you need. Your iPhone app will just use GET/POST requests to the scripts you have on your server. That script will do the MySQL connection and do whatever else needs to be done (check if they have entered the right password, or whatever) and then output something. The app will read that output and do whatever it needs to do.

Logan Serman
  • 29,447
  • 27
  • 102
  • 141