Am试图通过利用三个数据库表的信息来创建json数组。
利用此处的信息和解决方案source
我意识到我必须使用外键进行引用,还必须使用联接查询。
三个表的创建过程如下,每个表都有其参考键
Create table categorys(id int primary key auto_increment, category_name varchar(30), buyer_userid int(30));
Create table product(id int primary key auto_increment, product_name varchar(30), buyer_userid int(30),category_id int,
foreign key (category_id) references categorys(id));
Create table sales(items_id int primary key auto_increment, item_name varchar(30),quantity varchar(30), buyer_userid int(30),
product_id int, foreign key (product_id) references product(id));
这是我期望的Json格式...
[
{"id":"1","cat_name":"Provision","catbuy_userid":"100",
"products_info":[{"productid":"2","product_name":"Malt","buyer_userid":"100",
"sales_Info":[{"items_id":"1","item_name":"malt","buyer_userid":"100","quantity":"72"}]
}]},
{"id":"2","cat_name":"Cosmetics","catbuy_userid":"200",
"products_info":[{"productid":"3","product_name":"soapy","buyer_userid":"200",
"sales_Info":[{"items_id":"2","item_name":"cream","buyer_userid":"200","quantity":"83"}]
}]}
]
这是到目前为止我的编码
<?php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "mydb"; /* Database name */
$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$res_arr = array();
$query = '
SELECT
c.id,
c.category_name,
c.buyer_userid,
p.id,
p.product_name,
p.buyer_userid,
p.category_id,
s.items_id,
s.item_name,
s.quantity
FROM `categorys` c
INNER JOIN `product` p ON c.id=p.category_id
INNER JOIN `sales` s ON p.id=s.product_id
ORDER BY c.id';
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
$res_arr[] = $row;
}
echo json_encode($res_arr);
exit;
?>
这是我的json输出
[{"0":"1","id":"1","1":"Provisions","category_name":"Provisions","2":"100","buyer_userid":"100","3":"4","4":"Malt","product_name":"Malt","5":"100","6":"1","category_id":"1","7":"4","items_id":"4","8":"malt","item_name":"malt","9":"93","quantity":"93"},
{"0":"2","id":"2","1":"Cosmetics","category_name":"Cosmetics","2":"200","buyer_userid":"200","3":"3","4":"soapy","product_name":"soapy","5":"200","6":"2","category_id":"2","7":"3","items_id":"3","8":"cream","item_name":"cream","9":"83","quantity":"83"}
]
请按照上面的json格式循环播放结果。
更新部分
错误警告示例,当我运行代码时,它们是如此之多。下面只是其中的几个
警告:在第51行的C:\ xampp \ htdocs \ api \ test.php中使用未定义的常量products_info-假定为'products_info'(这会在PHP的未来版本中引发错误)
警告:在第51行的C:\ xampp \ htdocs \ api \ test.php中使用未定义的常量product_name-假定为“ product_name”(这会在PHP的未来版本中引发错误)
答案 0 :(得分:0)
当您说获得结果时,您是说要访问这些值吗?如果是这样,请将您的json数据分配给变量data
。
然后:
$json = json_decode($data);
foreach($json[0] as $values1) {
var_dump($values1);
}
foreach($json[1] as $values2) {
var_dump($values2);
}
例如,要从第一个对象获取产品名称,则需要执行$ values1-> product_name。对于其他属性,依此类推。
答案 1 :(得分:0)
这可以通过将(array) $row
转换为(object) $row
来实现:
$res_arr = array();
while($row = $result->fetch_assoc()){
array_push($res_arr, (object) $row);
}
header("Content-type: application/json; charset=utf-8");
die(json_encode($res_arr));
我会使用fetch_assoc()来防止这些重复编号的字段,这些字段只会使有效负载膨胀……整个嵌套基本上是无用的,并且只会产生垃圾代码质量,而没有什么地方比嵌套的嵌套多了项(因此,它比在客户端进行访问所必需的更为复杂)。例如,嵌套会有意义。首先获取所有categories
,然后为该products
嵌套所有category
,然后继续下一个category
。
答案 2 :(得分:0)
我对您的php代码进行了验证,因此您可以获得所需的输出。
根据您的要求,而不是从mysql查询返回的assignind行创建关联数组,然后转换为json
<?php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "mydb"; /* Database name */
$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$res_arr = array();
$query = '
SELECT
c.id,
c.category_name,
c.buyer_userid,
p.id,
p.product_name,
p.buyer_userid,
p.category_id,
s.items_id,
s.item_name,
s.quantity
FROM `categorys` c
INNER JOIN `product` p ON c.id=p.category_id
INNER JOIN `sales` s ON p.id=s.product_id
ORDER BY c.id';
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
$res['id'] = $row[0];
$res['cat_name'] = $row[1];
$res['catbuy_userid'] = $row[2];
$res['products_info']['id'] = $row[3];
$res['products_info']['productid'] = $row[4];
$res['products_info']['product_name'] = $row[5];
$res['products_info']['buyer_userid'] = $row[6];
$res['products_info']['sales_info']['items_id'] = $row[7];
$res['products_info']['sales_info']['items_name'] = $row[8];
$res['products_info']['sales_info']['buyer_userid'] = $row[6];
$res['products_info']['sales_info']['quantity'] = $row[9];
$res_arr[] = $res;
}
}
echo json_encode($res_arr);
?>