How to Connect an iOS App to a MySQL Database (Step by Step) - Part 4

preview_player
Показать описание
PHP Web Service Code:

In this video series, I'll show you how to connect an iPhone app to MySQL. We'll go through it step by step starting from the beginning.

You'll learn how to:
- Sign up for web hosting
- Create a new MySQL database
- Set up a MySQL database user
- Set access permission for that user
- Create a PHP web service to access the data
- Create an iOS app with a tableview, multiple screens and a map
- Access the PHP web service via the app to download the data from your MySQL database.

This is a blog version of the tutorial here:

For more tutorials on how to build iPhone apps, make sure you subscribe and visit my site where you'll find a community of like minded learners! Learning something new is always more fun with other people!

Get a customized roadmap for your app and start building it in 7 days:

CodeWithChris is dedicated to teaching beginners and non-programmers all about building iOS apps. On the site, you'll find a ton of free resources and tutorials to aid you on your journey to learn iOS development. Many people have successfully picked up Swift 3, Xcode 8 and app building from my course and materials!

Intro & Outro music
Рекомендации по теме
Комментарии
Автор

6 years later, still super useful. good work

CayenneNicolas
Автор

Thanks Chris for elaborating on how to create a simple web service for testing.

semilife
Автор

You are also teaching cybersecurity great man!!!

meetsodha
Автор

Hello. Thank you for the video. I followed the instructions but I only got the "HTTP ERROR 500" message. What did I miss?

KAUIEONG
Автор

Thanks Chris,

i am still new, And i created WordPress site, and if i want to see my information in (service.php) i found coming soon and i want find the information like yours, Also i want to save with ( .php ) extenuation but i can not suggested to me ( .rtf ) can you help me ..
To part 4 good, i want continua with your lessens

Thanks Dear :)

abdulrahmanal-dubaili
Автор

The first link to the PHP service is not available.

Please correct the text where it says "This is a video version of the tutorial here:" As thats the written version

hotmandead
Автор

Hi Chris, It seems the link for PHP Web Service Code is broken. Can you please fix it? Thank you.

mehradkavian
Автор

After running "mywebapp.com/service.php" I get a blank screen. It's not retrieving the data.

datawurx
Автор

The links to the other websites (with more info on php)?

lightbluedk
Автор

Why does the web service have to be in PHP? We can't we use Cocoa to interact with the db?

HDDude
Автор

After entering the service.php just a blank page can you help me ?

mertalptasdelen
Автор

6:35 - how do you save both files on the server?

shawntaylor
Автор

the php server file needs a header. So the server knows it's a .json file. Place header("Content-type", "application/json"); at the top of the php file.

willwright
Автор

You forgot to put the links of other articles in your descriptions...

ahmaliic
Автор

Thanks for this tutorial
i upload service.php to file manager and i test the URL browser but its nothing show me data
kindly guide me.

ahsanzahid
Автор

Hi Chris, awesome collection of videos. I recently stumbled up on this one while looking for a way to incorporate PHP files into MacOs/ios


I know this video is from 2017 but incase someone new watches it like me!. it might be worth them changing the PHP to incorporate Prepared statements (Better Safe then sorry).
Also If not able to do Prepared statements it would be a good idea to change the SELECT * (ALL clause) to specific tables/rows SELECT 'Name', 'Address', 'Latitude' etc


Below I have added a different approach using PHP, PDO and Procedural hopefully it might help one of your viewers with there project. (Hopefully!)


This is an example of Chris Code and should be change to meet your requirements:
(Please note this code may contain errors as I cant test it on a live server. Also its late and I have three more of Chris videos to watch.)


//Example (MySQLi Procedural)
//Why use MySQLi Procedural easy to write works great with mysql servers.


<?php
// connection to DB
$server_name = "localhost";
$user_name = "username";
$password = "password";
$dbname = "Database"


// Adding the variables to make the connection
$conn = mysqli_connect($server_name, $user_name, $password, $dbname);


// If no connection die and give error
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
// This echo can be deleted but used to show the connection is set up correctly




// prepare
$sql = "SELECT * (I would recommend listing the specific areas you want) FROM Locations";


if ($result = mysqli_query($conn, $sql)){
$resultArray = array();
$tempArray = array();


if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$tempArray = $row;
array_push($resultArray, $tempArray);

}
echo json_encode($resultArray);
} else {
// echo no results this echo can be deleted
echo "0 results";
}
}


mysqli_close($conn);




?>


// Procedural close connection should be used when ending your connections. mysqli_close($conn);





// Example using PDO
// Why use PDO works great with multiple servers.


<?php
$server_name = "localhost";
$user_name = "username";
$password = "password";


//Notice myDB in the query below that would need changing


try {
$conn = new PDO("mysql:host=$server_name;dbname=myDB", $user_name, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
// remember to change or delete the above echo if its not needed.
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
// select what you want from the database and execute that command
$stmt = $conn->prepare("SELECT * (I would recommend listing the specific areas you want) FROM Locations");
$stmt->execute();


// Setting variables as arrays
$resultArray = array();
$tempArray = array();

// fetch the data
while ($result =
foreach ($conn->query($sql) as $row) {
$tempArray = $row;
array_push($resultArray, $tempArray);
}
}


// This is added for error checking
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>


// close Connection for PDO $conn = null;


Again sorry if this has errors but it might help someone.

daynightstudio
Автор

I found that the service.php page is a blank page with nothing in it, and i don`t know what is going on...

gangwei
Автор

MySQL is ok but firebase is the next generation. There is many nice feature like notfications event and a lot more to help your for more comfort. I love it and your movies. thx a lot for that. God bless you

brazo
Автор

Ahh thank you for these vids!! I have a Senior Project to do on making an iOS app for an oil and gas company and I have never worked with xcode or Swift so these are a HUGE help!!

ViaVanille