4

I would like to clear my doubts in hooking up of MySQL database with XCode. My application would need to retrieve data from MySQL as there would be a login screen. As such, in order for me to retrieve data from my database, there is a need for me to create a database using MySQL and connect it using PHP and then connect PHP to XCode?

I am a greenhorn in application developing but I am tasked to do it for my school.

I would need great help in creating a PHP in connecting MySQL(it would be good if its steps-by-steps guide). I would really truly appreciate your kind generous reply.

Thank you in advance!

2
  • 1
    Why would you want to "connect PHP to Xcode"? That's like connecting Befunge to Vim. Commented Jan 18, 2012 at 9:02
  • like me i have website with its db already Commented Feb 4, 2017 at 23:56

4 Answers 4

3

It is very simple to connect to a MySQL database with PHP. There are a couple of APIs for this, mysql and mysqli. Mysqli is probably the better one to use, but mildly denser. The Mysql one works like this:

$db = mysql_connect("host:port", "username", "paswword");

mysql_select_db("my_db", $db);

# say we want to select everything from the table Persons
$result = mysql_query("SELECT * FROM Persons");

while ($row = mysql_fetch_array($result))
{
   # do your magic
   # columns are accessed in a zero based array
   # such as $row[0], $row[1], etc. 
   # look at mysql_fetch_assoc to see how to access
   # using the column names
}

mysql_close($db);

There's what looks like an older but still valid W3c tutorial here and the MySQL PHP API reference there. To learn about the API differences read the Overview of the MySQL PHP drivers.

As the other answers have stated you'll want the PHP to output something like JSON or XML to communicate with your app and the XCode.

Sign up to request clarification or add additional context in comments.

Comments

2

This tutorial follows the whole process through step by step from creating a web service to implementing the web service in your app. I found it super easy to follow.

Part 1: http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app

Part 2: http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service

1 Comment

so are you able to come out with the webservice? By hook or by crook, I would need it out by the end of this month :(
1

It sounds like you need some sort of WebService. What you can do is just create your PHP pages and let them output a set format (say JSON or XML). Then in your Obj-C application just do the webrequests and parse the results.

There might be some existing solutions which you can use, Webservice is the keyword here.

Comments

0

Here what sounds better to connect to a mysql database, your best bet is to use JSON/SOAP/XML/PHP websevices to talk between your database and your app..

The reason database connection directly from the device is a bad idea, is that you have to enable global external access to it for it to work. You can keep your data safer by having scripts on your server do the communication to the database.

One example of how to do this is create PHP pages that export XML data as your mysql data export , and use GET POST methods to post data to PHP pages to write to your database..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.