api allow specify id
This commit is contained in:
parent
839f79eca2
commit
ba26402705
52
api.php
52
api.php
@ -3,18 +3,42 @@
|
|||||||
header('Content-type: application/json');
|
header('Content-type: application/json');
|
||||||
//载入数据库文件
|
//载入数据库文件
|
||||||
require("data.php");
|
require("data.php");
|
||||||
//查询随机一条记录
|
//检查是否有指定id
|
||||||
$sql = "SELECT * FROM soul ORDER BY RAND() LIMIT 1";
|
if (isset($_GET['id'])) {
|
||||||
|
$id = $_GET['id'];
|
||||||
|
//查询指定id的记录
|
||||||
|
$sql = "SELECT * FROM soul WHERE id = $id";
|
||||||
|
} else {
|
||||||
|
//查询随机一条记录
|
||||||
|
$sql = "SELECT * FROM soul ORDER BY RAND() LIMIT 1";
|
||||||
|
}
|
||||||
$result = mysqli_query($conn, $sql);
|
$result = mysqli_query($conn, $sql);
|
||||||
$row = mysqli_fetch_assoc($result);
|
//检查是否查询成功
|
||||||
//输出json
|
if ($result) {
|
||||||
echo json_encode(array(
|
$row = mysqli_fetch_assoc($result);
|
||||||
"code" => 0,
|
//检查是否找到记录
|
||||||
"message" => "OK",
|
if (mysqli_num_rows($result) > 0) {
|
||||||
"data" => array(
|
echo json_encode(array(
|
||||||
"id" => $row["id"],
|
"code" => 0,
|
||||||
"title" => $row["title"],
|
"message" => "OK",
|
||||||
"author" => $row["author"],
|
"data" => array(
|
||||||
"from" => $row["from"]
|
"id" => $row["id"],
|
||||||
)
|
"title" => $row["title"],
|
||||||
));
|
"author" => $row["author"],
|
||||||
|
"from" => $row["from"]
|
||||||
|
)
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
echo json_encode(array(
|
||||||
|
"code" => 1,
|
||||||
|
"message" => "No results found",
|
||||||
|
"data" => null
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo json_encode(array(
|
||||||
|
"code" => 2,
|
||||||
|
"message" => "Error executing query",
|
||||||
|
"data" => null
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user