phpiosmysqliphonexcode

I want to connect my mySQL database to my xcode project


I am trying to connect my mysql database to my xcode project, for a simple login view where users can put in their username and password. I have the database set up already but I need to use JSON or?

 <?php

 // Create connection
   $con=mysqli_connect("localhost","username","password","dbname");



 // Check connection
 if (mysqli_connect_errno())
 {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

 // This SQL statement selects ALL from the table 'Locations'
 $sql = "SELECT * FROM Locations";

 // Check if there are results
 if ($result = mysqli_query($con, $sql))
 {
 // If so, then create a results array and a temporary one
 // to hold the data
 $resultArray = array();
 $tempArray = array();

 // Loop through each row in the result set
 while($row = $result->fetch_object())
 {
    // Add each row into our results array
    $tempArray = $row;
    array_push($resultArray, $tempArray);
 }

 // Finally, encode the array to JSON and output the results
 echo json_encode($resultArray);
}

 // Close connections
 mysqli_close($con);
 ?>

Solution

  • Here is a good article on this:

    [1]: http://codewithchris.com/iphone-app-connect-to-mysql-database/#parsejson Xcode JSON

    I haven't spent too much time with xcode, but from what I know there is no native API/framework for connecting to a MySQL database with it (along with most other frameworks of the kind). So yes - you need to set up a website/server with a database that has a web page that generates JSON code. Then have the xcode send a web request to the server/webpage, store the JSON code in a variable, and then use whatever method xcodes have to use/manipulate the JSON data. You'll want to make sure that both the xcode and web server don't prevent one another from passing data with the appropriate security privileges.

    You can pass the data to your xcode app in whatever format you want as well - JSON, CSV, or even XML. It's whatever capabilities xcode has is what you should stick with. Really you could even make up your own data format... it's your application, do what you want with it, whatever is easiest to work with in xcode.