Search code examples
phppostman

PHP GET Query returning null on a database with data


I have a php script to query from a table LABCPD which has one entry, however the GET call in POSTMAN is retuning status OK with no errors but not returning any data, there are no syntax errors or permission / access issues:

Script:

<?php
/**
 * CPD Manual porject.
 * Files will be hosted on mooneycallans.com server
 * SQL inj. safe
 */

require_once 'headers.php';

// Add debugging output to console in POSTMAN and error log

error_reporting(E_ALL);
ini_set('error_reporting', 'E_ALL');

// Make suer the database on server we are reading / writing to as in UTF-8standard

   mysqli_set_charset('m558405_tests','utf8');
   



require_once 'headers.php';

// Create a connection to the databse 'm558405_tests DB 
$conn = new mysqli(hostName,usernamePlaceHolder,passwordPlaceHolder,databaseName);

$table = "LABCPD";


// Check DB connection and return  a JSON error if cannot connect

if ($conn->connect_error) {
    $errorResponse = array(
        'error' => 'Connection failed',
        'message' => $conn->connect_error
    );
    header('Content-Type: application/json');
    http_response_code(500); // Set the HTTP response code to indicate an internal server error
    echo json_encode($errorResponse);
    exit(); // Terminate the script
}



    if ($_SERVER['REQUEST_METHOD'] === 'GET'){
        if(isset($_GET['id'])) {
            /// Sfaely escape the user input and use a prepared statement
            $id = $conn->real_escape_string($_GET['id']);
    
            // Prepare an SQL statement with a placeholder
            $stmt = $conn->prepare("SELECT * FROM $table WHERE id = ?");
    
            // Bind the escaped user input to the placeholder in the SQL statement
            $stmt->bind_param("i",$id);
    
            // Execute the prepared statement
            $stmt->execute();
    
            // Get the result of the query
            $result = $stmt->get_result();
    
            // Fetch the data as an associative array
            $data = $result->fetch_assoc();
    
            // Close the prepared statement
            $stmt->close();
        } else {
            $data = array();
    
            // Prepare an SQL statement to fetch all rows
            $stmt = $conn->prepare("SELECT * FROM $table");
    
            // Execute prepared statement
            $stmt->execute();
    
            // Get the result of the query
            $result = $stmt->get_result();
    
            while ($d = $result->fetch_assoc()){
                $data[] = $d;
            }
    
            // Close the prepared statement
            $stmt->close();
        }
    
        // Return the JSON data
        exit(json_encode($data, JSON_THROW_ON_ERROR, 512));
    }

Database has one entry: Database with one row

My question is does it look like there is an issue with PHP snippet or could I be missing something else?


Solution

  • Thanks for the pointers. The issue was with the below line of code. When I removed the JSON_THROW_ON_ERROR , a GET request replies with all data from that query. The remote web server runs PHP 7.0, and JSON_THROW_ON_ERROR was added on PHP 7.3;

    exit(json_encode($data, JSON_THROW_ON_ERROR, 512));