The Best Way to Connect Your iOS App to MySQL Database (4 Steps)

Hi Chris,

Thanks for posting this article. I have been looking for similar for a project that I am working on for my startup. However I have two questions to ask:

  1. In the article you post an example of some PHP code used to connect with the MySQL database. Below the code it says:
    In line 4, you’ll want to change the following placeholders:
    -username : use the username of the db user that you set up in step 1
    -password : use the password for the db user
    -dbname : use the name of the database you created in step 1
    Read the comments in the code above to see what each line does.

My problem is… there are NO comments that I can see or find in the website to better understand how to make this work. can you please post the code and include the comments and perhaps some expamle so that I can understand what I need to modify?

fetch_object())
{
// Add each row into our results array
$tempArray = $row;
-username:
-password:
-dbname:
array_push($resultArray, $tempArray);
}

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

}

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

  1. This code is use to pull data from the MySQL server database is there code that will allow me to post data to the SQL server database?

The code snippet on the webpage is not the complete Php program. I too am trying to learn the entire code. What is missing is the mysql_connect statement (this is where the username, password and dbname parameters will be used), the database query, a loop that fetches rows one at a time and calls the fetch_object routine, and the definition of the array_push routine, at a minimum. Maybe more.

I worked backwards from the desired json output listed in the lesson and created a PHP program to produce that output from the Locations table in a MySQL database using MAMP 5.7 for Mac OS. I have not moved on to the Xcode app that uses this php program yet. All I did was match the output suggested in the lesson page. My service.php program follows: NOTE: Not sure how to make this php code “pretty” I simply copied and pasted it from my editor, and the newlines did not behave like I would have liked…

<?php // Print the HTML preamble echo ""; echo ""; // Set up the DB Parameters $dbhost = ; $dbname = ; $dbuser = ; $dbpass = ; // Connect to the DB $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname); // Select all the columns from the Locations table $query = "SELECT * FROM Locations" ; $result = $mysqli -> query($query) ; $row = $result -> fetch_assoc() ; // If there is at least one row, echo the opening json bracket if ($row) echo "[" ; while ($row) { // Echo the opening json brace echo "{"; // Echo the current Location columns echo '"Name":"' . $row["Name"] . '",' ; echo '"Address":"' . $row["Address"] . '",' ; echo '"Latitude":"' . $row["Latitude"] . '",' ; echo '"Longitude":"' . $row["Longitude"] . '"' ; // Echo the closing json brace echo "}"; // Select the next row from the table $row = $result -> fetch_assoc(); // Separate json rows with a comma, if there is another row if ($row) echo ','; } // Close the db connection $mysqli -> close() ; // Echo the closing json bracket echo "]" ; // Close the HTML page echo ""; echo ""; ?>

Hi @EricLark welcome to the community

Move this post to the App Development category.

This isn’t a tutorial request