PHP 仅文件 returns 一个(最新)JSON 对象
PHP file only returns one (latest) JSON object
在对我的服务器执行 GET 请求时,我设置的 .PHP 文件仅 returns 最新的 JSON 对象,而不是整个数组。我认为它正在覆盖数组,而不是添加到数组中,但我对 PHP 不太了解,可以指向正确的方向。
提前感谢您的帮助,代码如下。
<?php
$result = mysql_query("SELECT * FROM books WHERE uni_year = '$uni_year' AND uni_course = '$uni_course'") or die(mysql_error());
if(!empty($result)) {
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
// $result = mysql_fetch_array($result);
// while($row = mysql_fetch_array($result)){
// temp array
$books = array();
$books["list_id"] = $result["list_id"];
$books["book_title"] = $result["book_title"];
$books["uni_course"] = $result["uni_course"];
$books["uni_year"] = $result["uni_year"];
$books["book_author"] = $result["book_author"];
$books["book_price"] = $result["book_price"];
$books["book_year"] = $result["book_year"];
$books["isbn"] = $result["isbn"];
//}
// success
$response["success"] = 1;
// user node
$response["books"] = array();
array_push($response["books"], $books);
} else {
$response["success"] = 0;
$response["message"] = "num of rows bigger than zero";
}
} else {
$response["success"] = 0;
$response["message"] = "No product found";
echo json_encode($response);
}
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
echo json_encode($response);
}
?>
您只是从结果中获取第一条记录 ($result = mysql_fetch_array($result);
)。
您需要循环获取它们:
<?php
$result = mysql_query("SELECT * FROM books WHERE uni_year = '$uni_year' AND uni_course = '$uni_course'") or die(mysql_error());
if(!empty($result)) {
if (mysql_num_rows($result) > 0) {
$books = array();
while($row = mysql_fetch_array($result)){
$book = array();
$book["list_id"] = $row["list_id"];
$book["book_title"] = $row["book_title"];
$book["uni_course"] = $row["uni_course"];
$book["uni_year"] = $row["uni_year"];
$book["book_author"] = $row["book_author"];
$book["book_price"] = $row["book_price"];
$book["book_year"] = $row["book_year"];
$book["isbn"] = $row["isbn"];
$books[] = $book;
}
// success
$response["success"] = 1;
// user node
$response["books"] = $books;
} else {
$response["success"] = 0;
$response["message"] = "num of rows bigger than zero";
}
} else {
$response["success"] = 0;
$response["message"] = "No product found";
}
echo json_encode($response);
在对我的服务器执行 GET 请求时,我设置的 .PHP 文件仅 returns 最新的 JSON 对象,而不是整个数组。我认为它正在覆盖数组,而不是添加到数组中,但我对 PHP 不太了解,可以指向正确的方向。
提前感谢您的帮助,代码如下。
<?php
$result = mysql_query("SELECT * FROM books WHERE uni_year = '$uni_year' AND uni_course = '$uni_course'") or die(mysql_error());
if(!empty($result)) {
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
// $result = mysql_fetch_array($result);
// while($row = mysql_fetch_array($result)){
// temp array
$books = array();
$books["list_id"] = $result["list_id"];
$books["book_title"] = $result["book_title"];
$books["uni_course"] = $result["uni_course"];
$books["uni_year"] = $result["uni_year"];
$books["book_author"] = $result["book_author"];
$books["book_price"] = $result["book_price"];
$books["book_year"] = $result["book_year"];
$books["isbn"] = $result["isbn"];
//}
// success
$response["success"] = 1;
// user node
$response["books"] = array();
array_push($response["books"], $books);
} else {
$response["success"] = 0;
$response["message"] = "num of rows bigger than zero";
}
} else {
$response["success"] = 0;
$response["message"] = "No product found";
echo json_encode($response);
}
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
echo json_encode($response);
}
?>
您只是从结果中获取第一条记录 ($result = mysql_fetch_array($result);
)。
您需要循环获取它们:
<?php
$result = mysql_query("SELECT * FROM books WHERE uni_year = '$uni_year' AND uni_course = '$uni_course'") or die(mysql_error());
if(!empty($result)) {
if (mysql_num_rows($result) > 0) {
$books = array();
while($row = mysql_fetch_array($result)){
$book = array();
$book["list_id"] = $row["list_id"];
$book["book_title"] = $row["book_title"];
$book["uni_course"] = $row["uni_course"];
$book["uni_year"] = $row["uni_year"];
$book["book_author"] = $row["book_author"];
$book["book_price"] = $row["book_price"];
$book["book_year"] = $row["book_year"];
$book["isbn"] = $row["isbn"];
$books[] = $book;
}
// success
$response["success"] = 1;
// user node
$response["books"] = $books;
} else {
$response["success"] = 0;
$response["message"] = "num of rows bigger than zero";
}
} else {
$response["success"] = 0;
$response["message"] = "No product found";
}
echo json_encode($response);