浏览器无法显示从MySQL查询构造的JSON值

时间:2016-01-11 03:15:54

标签: php json

<?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);
}
?>

没有连接问题,查询没问题。有人可以帮助我,为什么浏览器不能显示学生的名字?

我尝试了以下内容:

  • 在地址栏中键入?classid = 1,最后显示&#34;必填字段缺失&#34;哪个是对的
  • 在地址栏中键入?classid = 1,并且没有显示,没有错误。它应该显示学生姓名吗?

编辑1:好的,我找到了部分解决方案。我试着添加这个。

var_dump($response, json_last_error() === JSON_ERROR_UTF8);

它确实返回了boolean true。有什么想法来解决这个问题吗?

编辑2:好的,我在这里找到了解决方案。 Why would json_encode returns an empty string。谢谢!

1 个答案:

答案 0 :(得分:0)

我尝试过,一切似乎都很好。但未能复制您的问题。

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);
    }

请使用更多回声调试代码。通过示例数据库输入,我得到了上述代码的响应。

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"}]}