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:


    //  connection.php
    //  Created by gg1 on 10/04/12.
    //  Copyright (c) 2012 All rights reserved.
    //  Connect to the DB

    /** MySQL database name */
    define('DB_NAME', 'databasename');
    /** MySQL database username */
    define('DB_USER', 'username');
    /** MySQL database password */
    define('DB_PASSWORD', 'databasepassword');
    /** MySQL hostname */
    define('DB_HOST', 'databasehost');

        // Initialization
    $conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
    mysql_select_db(DB_NAME, $conn);

    // disable reporting errors for security reason

    // Error checking
    if(!$conn) {
        die('Could not connect ' . mysql_error());

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



    $table = "accounts";

    // Build the sql query
    $sql = "SELECT * FROM $table ";

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

    if(!$result) {
        die("Error retrieving scores " . mysql_error());
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

<html xmlns="">
    <meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
            // Display the table
            echo '<table style="width:100%">
           while ($row = mysql_fetch_object($result)) {
                echo '<tr>
            echo '</tbody>

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,


You may also like...

30 Responses

  1. ing conti says:

    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. admin says:

    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. Julius says:

    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. webcoderph says:

    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. admin says:

    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. warner says:

    i have an error that strResult is an unused variable:(

  7. admin says:

    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];

  8. Charles says:

    After pasting your code, I get this error:
    Use of undeclared identifier 'data'
    How do I fix this issue?

  9. Charles says:

    The error is in this line of code
     NSString *strResult = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

  10. admin says:

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


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

  11. Charles says:

    Thanks! Worked like a charm.

  12. Charles says:

    I want to take the *strResult and populate a TableView with it. Can you give me some pointers?

  13. Charles says:

    It is giving me a Bad Gateway 502 error. Will have to check this ayer once they get it fixed. Thanks again!

  14. Charles says:

    Both of those links are dead. :-( How can I go about using the strResult variable? Id like to plug it into a Table View or a drop down menu.

  15. Juninho De Luca/Brazil says:

    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!

  16. Fetom says:


    I have found a simple solution for your problem. I tried the sqlgw package from

    and it works fine.

  17. admin says:

    It’s java….
    It costs a tot….
    I cannot see the source.

  18. Max says:

    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.


  19. admin says:

    @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.

  20. Bianca says:

    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.

  21. Giovanni says:


    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>



  22. admin says:


    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.

  23. g says:

    is it possible to use this aproach to insert data from app into remote db

  24. admin says:

    Yes, you can use this approach!

  25. g says:

    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?

  26. admin says:

    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”]];



  27. g says:

    Great, thank you

Leave a Reply

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