1

I'm trying to connect a MySQL database via PHP to an iPhone app. I'm following this tutorial http://codewithchris.com/iphone-app-connect-to-mysql-database/

This is my first time doing anything like this, so I'm sorry if I'm leaving out some key information.

Here's the php file that I uploaded to the database on Bluehost

<?php

// Create connection
$con=mysqli_connect("localhost","gaethrco_travis","Energy=mc^2","gaethrco_data");

// 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);
?>

Here's the plist in Xcode

Here's the plist in Xcode

Here's Bluehost

Here's Bluehost

My problem is that when I go to my website gaethr.com/service.php nothing happens. According to the tutorial, this should happen

This is what should happen

Let me know if I'm leaving out some key information.

Theodore.K
  • 384
  • 2
  • 7
  • 21
  • This question has nothing to do with iOS, Swift 3, or Xcode 8, beta 2. I'm removing those tags. – Alexander Nov 23 '16 at 01:45
  • You should check if the query succeeded - it's possible the connection was good, but the query failed (for example, `Locations` table doesn't exist): `if ($result = mysqli_query($con, $sql)) { ... } else { echo mysqli_error($con); }` *or something* – HPierce Nov 23 '16 at 02:07

1 Answers1

0

First validate $con

if (!$con) {
  die("Connection error: " . mysqli_connect_errno());
}

Then validate recordset

if ($result = mysqli_query($con, $sql))
{
   ....
}
else {
   echo "No results";
}

Then ..

if(!empty($result) {
    while ($row = mysqli_fetch_array($result)) {
...
Jorge Londoño
  • 588
  • 2
  • 14