请帮助,我需要从两个表获取数据,然后在JSON中进行编码,我的表是
-
Customers_table
id |Inv_id |customer_name |地址|total_value
-
Items_table
id |Inv_id |描述|详细信息|数量|unit_price
我的PHP代码如下
<?php
require "config.php";
$sql = "SELECT customers_table.id,
customers_table.inv_id,
customers_table.customer_name,
customers_table.address,
customers_table.total_value,
items_table.inv_id,
items_table.description,
items_table.details,
items_table.inv_id,
items_table.qty,
items_table.ubit_price,
items_table.amount,
FROM customers_table INNER JOIN items_table ON
customers_table.inv_id = items_table.inv_id;";
$result = mysqli_query($db, $sql);
$response = array();
while ($row = mysqli_fetch_array($result))
{
array_push($response);
}
echo json_encode(array(
"server_response" => $response
));
mysqli_close($db)
?>
但是JSONLINT响应是
{
"server_response": []
}
请,我在做什么错?
array_push()
接受两个参数,例如此array_push($array, $element_to_be_pushed)
。array_push参考
您可以使用$array[]
重写相同的代码,这将把新元素推向下一个可用索引。
尝试此代码:
<?php
require "config.php";
$sql ="SELECT customers_table.id,
customers_table.inv_id,
customers_table.customer_name,
customers_table.address,
customers_table.total_value,
items_table.inv_id,
items_table.description,
items_table.details,
items_table.inv_id,
items_table.qty,
items_table.ubit_price,
items_table.amount
FROM customers_table INNER JOIN items_table ON
customers_table.inv_id = items_table.inv_id;";
$result = mysqli_query($db, $sql);
$response = array();
while($row = mysqli_fetch_array($result))
{
$response[] = $row;
}
echo json_encode(array("server_response"=> $response));
mysqli_close($db)
?>
更新答案:
额外逗号items_table.amount,
删除。