Using iOS to retrieve data from a remote mysql database

One of the most important and common job, for mobile applications is to talk with a remote database. Today about the 90% of the apps are interfacing a remote RDBMS, for example companies management applications or simple applications like the weather application, the social networks applications etc etc….

Often iOS applications have to interface a web site or an online db; to implement such communications, foundamentally, you can follow two streets:

  • Use a library that talks with the RDBMS

On the internet I've seen a lot of libraries, for iOS, that do the work. For example you can use mysql_connector/C from (take a look to this interesting post) or you can use something like iOS Open Database Connectivity SDK

  • Use scripts (php, perl, ruby etc etc) that are stored on the same server of the RDBMS

You can also talk with the RDBMS using scripts, this method has a lot of advantages:

  1. Security: The DB NAME, PASSWORD and HOST NAME aren't on the iOS device, so an attacker doesn't have them.
  2. Decoupling: The application code and the database code are decoupled, so if you need to change your application you don't have to recompile the interface, too. And vice-versa.
  3. Porting: if you want to port your application from a device to another, or to a iOS device to an Android device you don't have to recompile the library (if it exists).

Anyway there are also some disavantages, for example the CPU load is demanded to the server, because you are not using the device CPU to make computation. So keep in mind you can have this problem.

Now I'm going to show to you how to make a query to a MySQL DB using iPhone together with php scripts.

The connection to the database is the first problem you'll have, normally I write a php script that makes the connection and then I include it into each php script that works with the DB. Here you are the connection.php script:


I think this script is very simple so I'll go ahead without explanations.

Now if you want to retrieve the complete content of the "account" table you have to write a new script, let's call it accountlist.php


In your objective c code add the following few lines:

First of all prepare the request, 

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString@"http://www.yourdomain.ext/accountlist.php"]];



then issue the request,

NSError         * e;

NSData      *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&e];


last, retrieve the data

NSString *strResult = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

That's all,


Related Posts

30 thoughts on “Using iOS to retrieve data from a remote mysql database

  1. can be a very simple example, but lacks a lot of features need to work decently
    Apart form error checking in PHP, synch download in IOS is the evil.. apple says.
    Feel free to contact me if You need further  details.

  2. Hi Mr. conti,
    this is only a sample, it’s not the bible.
    I want only to give some info for beginners.
    Also, remeber that Apple is a great company its guidelines are very useful but Apple is a company, Apple wants to drive developers to Apple’s goals.
    Anyway this blog is open, so if you want you can post your articles (I’ll put your name and a link to your site) or you can reply to this post.
    Have a good day,

  3. Mr Conti, could you elaborate why retrieving data this way is evil? Im a beginner and would like to know further detail. Also, how do you ensure data delivery whilst not overloading the server If many users are downloading the data at once?

  4. Hi
    after getting the result from the database. how would you display the result? i mean what class is the right to use? is it uitable view? im a professional web developer using php and ruby but im a new  on ios 😀
    i just want to understand what the class is the right to use with the response from a database. 
    thanks and more power

  5. Hi,
    It depends on what you are doing, if you are retrieving a dump of the db, you cold put it into a uitextview.
    If you are retrieving a list of users, you should put it into a uitableview, if you are getting data about aircrafts you should place each flying object in a mkmapview.

  6. Strange, you should have a warning. Ant it is normal, you have only set its value, so it is unused.
    If you try to print it the warning will go away.
    For example add the following line:
    NSLog(“%@”, strResult);
    NSString *strResult = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

  7. @Charles make sure you have added the following line before the error


    NSData      *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&e];

  8. Man… tyvm…
    I have no idea how many time i have looking for a simple sample about to use PHP as layer to connect with remote MySQL.
    Thks again!

  9. This is really not a good example. Generating HTML on the server assumes that your app(s) will display data without any understanding of it. 

    A much more flexible architecture would be to return the data as JSON and format it on the client.

    If your database is in the cloud, take a look at Espresso Logic, EmergentOne or Kumulos. There is no excuse for writing this code by hand when you can just leverage a service.


  10. @Max this is your opinion and I publish it! Depending on what you are doing there are no excuses for use third parties (with high prices) architectures instead of building your own one.

  11. Regarding the disadvantage of using script to retrieve data from DB, is it really bad? Will demanding the CPU Load to server be able to cause a server down? I am new to iOS development so I want to know the best way of doing this.

  12. Hi!

    thanks for your tutorial!! But … I have a problem!!


    I changed accountlist.php to get data I need, but when i Log my results I have something like:


    2014-06-15 01:28:26.467 ProvaPhp[12784:60b] Risultati: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"



    <html xmlns=""&gt;


        <meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>



            <table style="width:100%">









                            Frase uno, Prova!






                            Frase due, Prova!





                    </table>    </body>



  13. @Giovanni,

    I cannot understand, what is the error in your test?

    in strResult you should have the results from database in an HTML page, and…. you have this page, now you have to parse the results.

    Let me know if you are talking about a different problem.

  14. So, do I have to compose your accountlist.php as NSString to include, say $sql = "INSERT INTO $table ('col1','col2') VALUES('val1','val2')"; statement instead of SELECT etc, save/upload it as .php file up on a server then fire up NSURLRequest on that php file? Something like that?

  15. You can create a new php file accepting a parameter like “account_name”

    let’s call it addaccount.php

    < ?php

        $table = “accounts”;

        // Build the sql query
        $sql = “INSERT …….. ” . $_GET[“account_name”];

        $result = mysql_query($sql,$conn);

        if(!$result) {
            die(“Error retrieving scores ” . mysql_error());

    In the objc you have to change the line 

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString@”http://www.yourdomain.ext/accountlist.php”]];


    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString@”http://www.yourdomain.ext/addaccount.php?account_name=youraccountname”]];



Leave a Reply

Your email address will not be published. Required fields are marked *