gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:54:29 25 4
gpt4 key购买 nike

<分区>

<?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 的地址栏中输入,最后显示“必填字段缺失”,这是正确的
  • 在地址栏中输入 ?classid=1 最后没有显示,没有错误,这是错误的。它应该显示学生的名字吗?

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

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

它确实返回了 boolean true。有解决此问题的想法吗?

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com