我创建了一个api来从数据库中获取数据以将其作为json回显,最后在html中显示它,但在回显数据时,
我的PHP错误
注意:未定义的偏移量:第16行的/opt/lampp/htdocs/ReadExchange/api.php中的80
注意:未定义的偏移量:第16行/opt/lampp/htdocs/ReadExchange/api.php中的79
注意:未定义的偏移量:第16行/opt/lampp/htdocs/ReadExchange/api.php中的78
等等,
PHP代码
$db_result_set = $db->prepare('SELECT p.Id, p.FirstName, p.MiddleName, p.LastName, p.Gender, p.Location, p.Email, p.Mobile,b.BookTitle, b.BookGenre, b.BookWriter, b.BookDescription FROM personaldetails AS p LEFT JOIN bookdetails AS b ON b.UserId = p.Id ORDER BY p.Id DESC');;
$db_result_set->execute();
$final = [];
foreach ($db_result_set as $u) {
if (!is_array($final[$u["Id"]])) { //line 16
$final[$u["Id"]]= [
"Id" => $u["Id"],
"FirstName" => $u["FirstName"],
"MiddleName" => $u["MiddleName"],
"LastName" => $u["LastName"],
"Gender" => $u["Gender"],
"Location" => $u["Location"],
"Email" => $u["Email"],
"Mobile" => $u["Mobile"],
];
}
$final[$u["Id"]]["books"][] = [
"BookTitle" => $u["BookTitle"],
"BookGenre" => $u["BookGenre"],
"BookWriter" => $u["BookWriter"],
"BookDescription" => $u["BookDescription"],
];
}
header('Content-type: application/javascript');
echo json_encode($final);
>
我不确认,但我认为因为这个我没有得到html中的json数据。
答案 0 :(得分:1)
不是:if (!is_array($final[$u["Id"]]))
但是:if (!isset($final[$u["Id"]]))
答案 1 :(得分:0)
您正在尝试检查数组中是否存在未定义的偏移量。您首先需要检查isset
是否存在偏移量。
所以将代码更改为
if (isset($final[$u["Id"]]) && ! is_array($final[$u["Id"]))