请帮忙,我需要从两个表中提取数据,并用json编码我的表是
Customers_table
id |库存id |客户名称|地址|总价值
Items_table
id |库存id |描述|明细|数量|单价
我的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 reference
您可以使用array_push($array, $element_to_be_pushed)重写相同的代码,这将把新元素推送到下一个可用的索引。
请尝试以下代码:

<?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)
?>

更新答案:
删除了额外的逗号“$array[]”。

07-26 01:10