<?php
include_once 'connection.php';
class JsonEncode {
private $db;
private $connection;
function __construct() {
$this -> db = new DB_Connection();
$this -> connection = $this->db->getConnection();
}
public function get_class_list()
{
$response = array();
$classid = $_GET['classid'];
$result = mysqli_query(
$this->connection,
"SELECT student.idno, person.firstname, person.lastname
FROM person, student, enrolls
WHERE enrolls.classid=$classid
AND enrolls.sidno=student.idno
AND student.pid=person.id
ORDER BY person.lastname ASC");
if (!empty($result)) {
// check for empty result
if (mysqli_num_rows($result) > 0) {
$response["students"] = array();
while ($row = mysqli_fetch_array($result)) {
// temp user array
$student = array();
$student["idno"] = $row["idno"];
$student["firstname"] = $row["firstname"];
$student["lastname"] = $row["lastname"];
// push single student into final response array
array_push($response["students"], $student);
}
// echoing JSON response
echo json_encode($response);
}
else {
// no product found
$response["success"] = 0;
$response["message"] = "No student found";
// echo no users JSON
echo json_encode($response);
}
}
mysqli_close($this -> connection);
}
}
$jsonencode = new JsonEncode();
if(isset($_GET["classid"])) {
$jsonencode-> get_class_list();
}
else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
No Problem with connection, no problem with query. Can someone help me why can't the browser display the name of students?
I have tried the following:
- Typed in the address bar without ?classid=1 and at the end it displayed "Required Field is Missing" which is correct
- Typed in the address bar with ?classid=1 at the end and it displayed nothing, no errors which is wrong. It should display the students names right?
Edit 1: Ok, I've found a partial solution. I tried to add this.
var_dump($response, json_last_error() === JSON_ERROR_UTF8);
and it did return boolean true
. Any ideas to fix this?
Edit 2: Ok, I've found the solution here. Why would json_encode returns an empty string. Thanks!