如何执行几个 mysqli 查询并将一个结果添加到现有结果数组中?
How to perform couple of mysqli queries and add one result into existing result array?
需要执行几个 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 directors_table WHERE director_id = $dir_id"
并将结果作为导演对象添加到json响应中,
{
"status": "success",
"movies": [
{
"id": "8",
"image": "image-url-here",
"language": "english",
"title": "avengers",
"year": "2005",
"director": {
"id": "152",
"name": "director",
"age": 50
}
}
]
}
两种解决方案
像@AymDev 在对您的问题的第一条评论中建议的那样进行 JOIN。如果您的表相对较小并且没有任何性能问题,这可能是首选解决方案
双查询
// First retrieve all the directors and keep an array with their info. The Key of the array is the director ID
$dataQuery = "SELECT * FROM directors_table";
$sth = mysqli_query($conn, $dataQuery);
$directors = array();
while($r = mysqli_fetch_assoc($sth)) {
$directors[$r['id']] = $r;
}
$dataQuery = "SELECT * FROM movies_table";
$sth = mysqli_query($conn, $dataQuery);
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
// Retrieve the director info from the previous array
$r['director'] = $directors[$r['dir_id']];
$rows[] = $r;
}
$respObj->status = 'success';
$respObj->movies = $rows;
$respJSON = json_encode($respObj);
print $respJSON;
在您的查询中使用 JOIN
:
SELECT *
FROM movies_table m
INNER JOIN directors_table d ON d.director_id = m.dir_id
并在循环中构建数组结构:
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = [
'id' => $r['id'],
'image' => $r['image'],
/* other movie keys you need */
'director' => [
'id' => $r['director_id'],
/* other director keys you need */
]
];
}
需要执行几个 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 directors_table WHERE director_id = $dir_id"
并将结果作为导演对象添加到json响应中,
{
"status": "success",
"movies": [
{
"id": "8",
"image": "image-url-here",
"language": "english",
"title": "avengers",
"year": "2005",
"director": {
"id": "152",
"name": "director",
"age": 50
}
}
]
}
两种解决方案
像@AymDev 在对您的问题的第一条评论中建议的那样进行 JOIN。如果您的表相对较小并且没有任何性能问题,这可能是首选解决方案
双查询
// First retrieve all the directors and keep an array with their info. The Key of the array is the director ID
$dataQuery = "SELECT * FROM directors_table";
$sth = mysqli_query($conn, $dataQuery);
$directors = array();
while($r = mysqli_fetch_assoc($sth)) {
$directors[$r['id']] = $r;
}
$dataQuery = "SELECT * FROM movies_table";
$sth = mysqli_query($conn, $dataQuery);
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
// Retrieve the director info from the previous array
$r['director'] = $directors[$r['dir_id']];
$rows[] = $r;
}
$respObj->status = 'success';
$respObj->movies = $rows;
$respJSON = json_encode($respObj);
print $respJSON;
在您的查询中使用 JOIN
:
SELECT *
FROM movies_table m
INNER JOIN directors_table d ON d.director_id = m.dir_id
并在循环中构建数组结构:
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = [
'id' => $r['id'],
'image' => $r['image'],
/* other movie keys you need */
'director' => [
'id' => $r['director_id'],
/* other director keys you need */
]
];
}