-2

I'm just trying to get data from a table in a MySQL database, but outputting the result after running mysqli_fetch_all($result, MYSQLI_ASSOC); returns {"current_field":null, "field_count":null, "lengths":null, "num_rows":null, "type":null}. I haven't had a problem with this before, as I was getting correct data back before. Other queries that would return true like INSERT and UPDATE work fine, and there are records in the table. I am, however, pretty new to PHP, so I could be missing something obvious.

<?php

include("APIBase.php");

//Get query from POST request
//$content = trim(file_get_contents("php://input"));
//$decoded = json_decode($content, true);
//$query = $decoded["apiData"];

if($_SERVER["REQUEST_METHOD"] == "GET"){
    //exit("yeahhhh no");
    $query = "SELECT * FROM `user_accounts`"; //this line for testing purposes
}

//connect to database
$conn = mysqli_connect("localhost", "id14495771_root", "Thereisnopa55word$", "id14495771_user_info");
$result = mysqli_query($conn, mysqli_escape_string($conn, $query));

//check result
if($result == false){
    exit(APIResponse(true, mysqli_error($conn), array()));
}
else if($result == true){
    $data = $result;
}
else {
    $data = mysqli_fetch_all($result, MYSQLI_ASSOC);
}

//free and close connection
mysqli_free_result($result);
mysqli_close($conn);

//return data
$output = APIResponse(false, "", $data);
exit($output);
?>

APIBase.php:

<?php 

header("Access-Control-Allow-Origin: *");

function APIResponse(bool $isError, $errorMessage = "", $data){
    $output = array("isError" => $isError, "errorMessage" => $errorMessage, "data" => $data);
    exit(json_encode($output));
}

?>
  • I think your problem in `if else` statement. – Muhammad Farras Ma'ruf Jun 14 '21 at 03:49
  • I removed the if` and else, and just had $data = mysqli_fetch_all($result, MYSQLI_ASSOC);, and it gave me the correct data, but I'm not why. Is it because $data is local? But it would've given me an error then, right? @MuhammadFarrasMa'ruf – Schuyler Henry Jun 14 '21 at 03:59
  • Ohhh wait, I understand. In my `else if` statements, I was using == instead of === so `$result==true` was true. – Schuyler Henry Jun 14 '21 at 04:09
  • Do not use `mysqli_escape_string`. Especially not like you are doing it right now – Dharman Jun 14 '21 at 10:46
  • Don't check for true or false. Remove the `if`s and enable error reporting [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Jun 14 '21 at 10:46

3 Answers3

0

In my else if statements, I was using == instead of ===. So after type-juggling, $result==true came back as true. To fix this, just replace == with === to check type as well.

0

Do a strict comparison for the if else statement.

mk23
  • 1,257
  • 1
  • 12
  • 25
0

if you got a results or not.. this condition

( $result  == true ) 

will be always your case.

the result variables there is some property and methods belong to A class Php Doc

If you assign the address of the result to the data it will be lost after free result and closing the connection.

the best ways is loop through result using fetch and fill an array or an object dynamically. the you free the result.

Or maybe you have to clone the result not just assign it as any other variables try it maybe it works.