Unable to access response from IONOS MySQL Database

698 views Asked by At

I'm trying to deploy a project to a web server. I've successfully created the database and I can see the response array is received in the 'network' tab of developer tools in Chrome, however no matter what I seem to try I cannot seem to be able to access this response array in my javascript to then display in the front end. I'm very stuck as to what the issue is. I'm assuming this can't be an issue with Ionos as I'm receiving the response in the network tab.

getAll.php

<?php

    // example use from browser
    // http://localhost/companydirectory/libs/php/getAll.php

    // remove next two lines for production
    
    ini_set('display_errors', 'On');
    error_reporting(E_ALL);

    $executionStartTime = microtime(true);

    include("config.php");

    //header('Content-Type: application/json; charset=UTF-8');

    $conn = new mysqli($host_name, $user_name, $password, $database);

    if (mysqli_connect_errno()) {
        
        $output['status']['code'] = "300";
        $output['status']['name'] = "failure";
        $output['status']['description'] = "database unavailable";
        $output['status']['returnedIn'] = (microtime(true) - $executionStartTime) / 1000 . " ms";
        $output['data'] = [];

        mysqli_close($conn);

        echo json_encode($output);

        exit;

    }   

    // SQL does not accept parameters and so is not prepared

    $query = 'SELECT p.id, p.lastName, p.firstName, p.jobTitle, p.email, p.departmentID, d.name as department, l.name as location, l.id as locationID FROM personnel p LEFT JOIN department d ON (d.id = p.departmentID) LEFT JOIN location l ON (l.id = d.locationID) ORDER BY p.lastName, p.firstName, d.name, l.name';

    $result = $conn->query($query);
    
    if (!$result) {

        $output['status']['code'] = "400";
        $output['status']['name'] = "executed";
        $output['status']['description'] = "query failed";  
        $output['data'] = [];

        mysqli_close($conn);

        echo json_encode($output); 

        exit;

    }
   
    $data = [];

    while ($row = mysqli_fetch_assoc($result)) {

        array_push($data, $row);

    }

    $output['status']['code'] = "200";
    $output['status']['name'] = "ok";
    $output['status']['description'] = "success";
    $output['status']['returnedIn'] = (microtime(true) - $executionStartTime) / 1000 . " ms";
    $output['data'] = $data;
    
    mysqli_close($conn);

    echo json_encode($output); 

?>

script.js

 function getEmployeeData() {
$.getJSON("php/getAll.php", function (data) {
    let employees = data.data;
    console.log(employees);
    populateEmployeeTab(employees);
    populateEmployeesInDeptLists(employees);
    populateEmployeeInLocation(employees);
    currentData = employees;
  });

}

1

There are 1 answers

2
Ap3rtur3 On

Maybe you need another request function than $.getJSON (like $.get) and parse the response yourself with JSON.parse(), but I don't know exactly what the response is.

Can you post the output of this command?

$.getJSON("php/getAll.php", function (data) {
  console.log(typeof data, data);
});