3
<?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!

Community
  • 1
  • 1
Jhii
  • 117
  • 3
  • 14

1 Answers1

0

I have tried and everything seems fine. But failed to replicate your issue.

public function get_class_list()
    {
        $response = array();
        error_reporting(-1);
        ini_set('display_errors', true);
        $classid = $_GET['classid'];
        $query = "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";
        $result = mysqli_query($this->connection, $query);
        echo "Query: <br/> $query <br/>";
        if (!empty($result)) {
            // check for empty result
            $noOfRows = mysqli_num_rows($result);
            echo "num of rows: $noOfRows <br/>";
            if ($noOfRows > 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 "<br/><br/>".json_encode($response);
            }
            else {
                // no product found
                $response["success"] = 0;
                $response["message"] = "No student found";

                // echo no users JSON
                echo "<br/><br/>".json_encode($response);
            }
        } else {
            "Empty result";
        }

        mysqli_close($this -> connection);
    }

Please debug the code with more echo. With a sample DB input i got a response like this for the above code.

Query: 
SELECT student.idno, person.firstname, person.lastname FROM person, student, enrolls WHERE enrolls.classid = 2 AND enrolls.sidno=student.idno AND student.pid=person.id ORDER BY person.lastname ASC 
num of rows: 1 


{"students":[{"idno":"111","firstname":"test11","lastname":"test12"}]}
Santhy K
  • 829
  • 1
  • 7
  • 12
  • This is what I get: Query: SELECT student.idno, person.firstname, person.lastname FROM person, student, enrolls WHERE enrolls.classid = 2 AND enrolls.sidno=student.idno AND student.pid=person.id ORDER BY person.lastname ASC num of rows: 27 – Jhii Jan 11 '16 at 11:15