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