0

Sorry to ask what I assume is a fairly simple question. Okay, so I'm trying to follow https://www.skysilk.com/blog/2018/how-to-connect-an-android-app-to-a-mysql-database/, a tutorial on how to do, well, exactly what it says. However, my PHP code, when I test it the way they say I should, by loading up "herokuserverbeingused.com/phpcode.php" in my browser, instead of getting the echo it says I should, I instead get nothing. Here's my code, it's literally just the code from the tutorial modified slightly

<?php
//borrowing from don't forget to credit https://www.skysilk.com/blog/2018/how-to-connect-an-android-app-to-a-mysql-database/

$con=mysqli_connect("a heroku server.net","nope","sorry","very secret");

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

$sql = "SELECT * FROM players";

// Confirm there are results
if ($result = mysqli_query($con, $sql))
{
    // We have results, create an array to hold the results
    // and an array to hold the data
    $resultArray = array();
    $tempArray = array();

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

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

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

I'm using Heroku to host everything because my professor said that would be a good idea, is that what's causing the problem?

ksmiles
  • 91
  • 1
  • 10
  • Have you edited the parameters here `$con=mysqli_connect("a heroku server.net","nope","sorry","very secret");` and changed the table `players`? –  Jul 29 '18 at 19:33
  • If so, Add the following code at the top of the PHP file to display errors `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); display_errors = on;` or check the `errors_log` file –  Jul 29 '18 at 19:37
  • Yeah, I edited the params to keep y'all from actually looking at my database lol. Thanks for the advice, I'll add that. – ksmiles Jul 29 '18 at 19:57
  • Update: the error log I can view via heroku log says 'unexpected '=' in filename.php on line 5' when I add that, line five being display_errors = on;. When I remove that line, it returns to having no errors visible via heroku log, but also not echoing anything, or at least not anything visible. – ksmiles Jul 29 '18 at 20:07
  • What code is on line 5? –  Jul 29 '18 at 20:12
  • I said, line five is display_errors=on. When commented out, the code runs, but it doesn't echo. I added a error_log("testecho") to the code to see if the code is running at all and I am getting testecho in heroku log so the code is running, the issue has to be echo – ksmiles Jul 29 '18 at 20:15
  • Try `print_r(json_encode($resultArray));` or `var_dump(json_encode($resultArray););` –  Jul 29 '18 at 20:20
  • When I do that it returns bool(false) which is completely mystifying. – ksmiles Jul 29 '18 at 20:34
  • From PHP manual http://php.net/manual/en/function.json-encode.php `Returns a JSON encoded string on success or FALSE on failure.` –  Jul 29 '18 at 20:41
  • Try `print_r( $resultArray );` or `var_dump( $resultArray );` –  Jul 29 '18 at 20:43
  • That works. It's returning a bunch of data set up like Array ( [0] => stdClass Object ( [attribute] => value ) [1] => stdClass Object ( [attribute] => value )) I'll go take a look at the documentation for json_encode, see if I can figure out why it won't accept this as input. – ksmiles Jul 29 '18 at 20:48

1 Answers1

1

It turned out the issue was that the inner rows in $resultArray were stdClass Objects instead of arrays, the solution was to replace $temparray = $row with $tempArray = json_decode(json_encode($row), True);

Credit to Dan for helping me figure out what the issue was and the guy who answered Convert stdClass object to array in PHP for showing me the easiest solution

ksmiles
  • 91
  • 1
  • 10