【发布时间】:2021-08-18 14:34:28
【问题描述】:
需要执行几个mysqli查询并将一个结果添加到现有结果数组中,目前我已经实现了第一个查询,
$dataQuery = "SELECT * FROM movies_table";
$sth = mysqli_query($conn, $dataQuery);
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
$respObj->status = 'success';
$respObj->movies = $rows;
$respJSON = json_encode($respObj);
print $respJSON;
结果是这样的,
{
"status": "success",
"movies": [
{
"id": "8",
"image": "image-url-here",
"language": "english",
"title": "avengers",
"year": "2005",
"dir_id": "152"
}
]
}
现在我想执行另一个查询,
"SELECT * FROM director_table WHERE director_id = $dir_id"
并将结果作为director对象添加到json响应中,
{
"status": "success",
"movies": [
{
"id": "8",
"image": "image-url-here",
"language": "english",
"title": "avengers",
"year": "2005",
"director": {
"id": "152",
"name": "director",
"age": 50
}
}
]
}
【问题讨论】:
-
创建一个
JOIN? -
@AymDev 如果我使用 JOIN,json 结果结构将与我想要的不同