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!

7
  • what happens when you var_dump the value of $result ? Commented Jan 11, 2016 at 3:19
  • If it is false run if (!$result) mysqli_error($this->connection); Commented Jan 11, 2016 at 3:25
  • object(mysqli_result)[4] public 'current_field' => null public 'field_count' => null public 'lengths' => null public 'num_rows' => null public 'type' => null Commented Jan 11, 2016 at 3:26
  • Try your query directly in DB and check whether it have any result Commented Jan 11, 2016 at 3:29
  • What version of PHP are you using? Commented Jan 11, 2016 at 3:29

1 Answer 1

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"}]}
Sign up to request clarification or add additional context in comments.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.