【发布时间】:2021-02-23 00:12:44
【问题描述】:
我的 PHP 脚本应该从 API 接收不确定数量的 JSON 数据,将其存储在一个数组中,然后对数组中的每个项目运行 SQL 查询。然后将查询作为 JSON 对象返回。
我的 API 如下所示:
if($_POST(['id'])){
$id= $_POST['id'];
foreach($value as $id){
$sql = "SELECT id, header, body FROM `messages` WHERE id= $id";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $id);
$stmt->execute();
while ($row = mysqli_fetch_assoc($stmt)) {
$response = $row ['id'];
$response = $row ['header'];
$response = $row ['body'];
}
$stmt->close();
}
return json_encode($response);
}
对我来说,这似乎效率很低,必须有一个 SQL 函数来检查数组中的所有变量。
我需要我的 JSON 看起来像这样
{
"resoponse": {
"check": "check"
},
"code": [{
"id": "id",
"header": "header",
"body": "body"
},
{
"id": "id",
"header": "header",
"body": "body"
},
{
"id": "id",
"header": "header",
"body": "body"
}
]
}
我正在使用改造/gson 将 JSON 返回到运行 kotin 的 android 应用程序,所以如果我遗漏了什么,请告诉我。
感谢您的宝贵时间。
【问题讨论】:
-
如果是 id 的集合,可以使用 where in 子句