这是我的代码,它从xampp外部数据库中检索数据并将其存储到解析为json的数组
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/conectaDB.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
$result = mysql_query("select cd.id, cd.dt_ini_camp, cd.dt_fim_camp, cd.descricao, + (select sum(quantidade) from lin_doc where id_cab_doc = cd.id) as qtd from cab_doc cd where cd.id_tipo_doc = 1") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["id"] = $row["id"];
$product["dt_ini_camp"] = $row["dt_ini_camp"];
$product["dt_fim_camp"] = $row["dt_fim_camp"];
$product["descricao"] = $row["descricao"];
$product["qtd"] = $row["qtd"];
// push single product into final response array
$response["cab_doc"] = array();
array_push($response["cab_doc"], $product);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>
输出:
{"cab_doc":[{"id":"69","dt_ini_camp":"2012-12-22","dt_fim_camp":"2012-12- 26","descricao":"cadqadadad","qtd":null}],"success":1}
我的问题是,这是最后一条记录,我想要所有,最新消息请帮助
答案 0 :(得分:1)
尝试使用 while()
循环
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array(
'id' => $row["id"],
'dt_ini_camp' => $row["dt_ini_camp"],
'dt_fim_camp' => $row["dt_fim_camp"],
'descricao' => $row["descricao"],
'qtd' => $row["qtd"]
);
// push single product into final response array
array_push($response["cab_doc"], $product);
}
您可以尝试将其添加到 $response['cab_doc']
数组中,如下所示:
$response['cab_doc'][] = $product;
这对你有什么影响?
编辑:试试这个:
$response["cab_doc"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array(
'id' => $row["id"],
'dt_ini_camp' => $row["dt_ini_camp"],
'dt_fim_camp' => $row["dt_fim_camp"],
'descricao' => $row["descricao"],
'qtd' => $row["qtd"]
);
// push single product into final response array
array_push($response["cab_doc"], $product);
}
编辑#2:
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/conectaDB.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
$result = mysql_query("select cd.id, cd.dt_ini_camp, cd.dt_fim_camp, cd.descricao, + (select sum(quantidade) from lin_doc where id_cab_doc = cd.id) as qtd from cab_doc cd where cd.id_tipo_doc = 1") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["cab_doc"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array(
'id' => $row["id"],
'dt_ini_camp' => $row["dt_ini_camp"],
'dt_fim_camp' => $row["dt_fim_camp"],
'descricao' => $row["descricao"],
'qtd' => $row["qtd"]
);
// push single product into final response array
array_push($response["cab_doc"], $product);
//or
// $response['cab_doc'][] = $product;
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>