-1

Hello all I am tryin to build IOS app and I need to get data my MySQL database. I do not know php. I found a tutorial https://codewithchris.com/iphone-app-connect-to-mysql-database/ In section 3 where we create PHP service I copy it and edit for mine information. PHP code is like that

<?php

//create connection
$con=mysqli_connect("localhost","myuserid","mypassword","i4142489_wp1");

// 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 treelibrary";

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

?>

I load it the my server and nothings pops up just the blank page... Is there any reason or where is my mistake. Thanks for helps. Have a nice day.

Arnab
  • 3,267
  • 1
  • 26
  • 39

1 Answers1

0

I didn't read you're whole code, but I'm pretty sure you're trying to put the results into a JSON format and echo them.

Here's an easy way to do so;

<?php

//
// EDIT: dont use this code, scroll a little bit lower to see the edit I made.
//

//create connection
$con=mysqli_connect("localhost","myuserid","mypassword","i4142489_wp1");

// 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 treelibrary";

// Executes the SQL statement and puts results into $res
$res = $con->query($sql);

// Checks if there's any rows
if($res->num_rows > 0) {

    // Puts all results in $row
    $row = $res->fetch_assoc();

    // echo & encode datas
    echo json_encode($row);
} else {
    echo "no data found";
}

// Close connections
mysqli_close($con);

Edit: The code above does work but only echoes the first row. Here's one that actually echoes all of the rows in JSON format (tested).

<?php

//create connection
$con=mysqli_connect("localhost","myuserid","mypassword","i4142489_wp1");

// 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 treelibrary";

// Executes the SQL statement and puts results into $res
$res = $con->query($sql);

// Checks if there's any rows
if($res->num_rows > 0) {

    // defines $data
    $data = array();

    // grabs all data and adds them to the $data array
    while ($row =  $res->fetch_assoc()) {
        array_push($data, $row);
    }

    // echo & encode datas
    echo json_encode($data);
} else {
    echo "no data found";
}

// Close connections
mysqli_close($con);
Mxlvin
  • 242
  • 3
  • 9
  • I tried your code now but the same result maybe my `$con=mysqli_connect("localhost","myuserid","mypassword","i4142489_wp1");` line is wrong – Mertalp Tasdelen Sep 30 '18 at 13:15
  • 1
    `Puts all results in $row` is incorrect, that only puts the first row in. – user3783243 Sep 30 '18 at 13:16
  • Thanks @user3783243 fixed it. – Mxlvin Sep 30 '18 at 13:46
  • @user3783243after using the edited version of code resul is the blank page – Mertalp Tasdelen Sep 30 '18 at 14:56
  • Then it's probably an error on your side, either you have no content in your database, of your database information is incorrect or you have some other code that's stopping it... Because I've tried it again, and it's still working... Something should always be printed on your screen because there's an IF & ELSE statement, and both has `echo()`. If you have discord, you can leave your discord tag and I'll dm you for further help. – Mxlvin Sep 30 '18 at 15:12
  • @iStudLion my discord if Mertalp#7389 – Mertalp Tasdelen Sep 30 '18 at 18:32