Skip to content

Commit c9bcbc2

Browse files
authored
Merge pull request #148 from Steffen-MLR/master
Fix for status api by correcting sql queries
2 parents 184f803 + 002bb13 commit c9bcbc2

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

api/status.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,25 @@
1515
$array = $constellation->render_status(true, false);
1616
echo json_encode($array);
1717
}else{
18-
$query = $mysqli->prepare("SELECT name FROM services WHERE id=?");
19-
$query->bind_param("i", $_GET['id']);
20-
$query->execute();
21-
$result = $query->get_result()->fetch_assoc();
18+
// get id of service, check if service exists
19+
$queryId = $mysqli->prepare("SELECT id from services where id = ?;");
20+
$queryId->bind_param("i", $_GET['id']);
21+
$queryId->execute();
22+
$result = $queryId->get_result()->fetch_assoc();
2223
if (!count($result))
2324
{
2425
die(json_encode(["error" => _("Service does not exist!")]));
2526
}
26-
27-
$sql = $mysqli->prepare("SELECT type FROM services_status INNER JOIN status ON services_status.status_id = status.id WHERE service_id = ? AND `time` <= ? AND (`end_time` >= ? OR `end_time`=0) ORDER BY `time` DESC LIMIT 1");
28-
29-
$sql->bind_param("iii", $id, $timestamp, $timestamp);
30-
$sql->execute();
31-
$tmp = $sql->get_result();
32-
if ($tmp->num_rows)
33-
{
34-
$service = new Service($_GET['id'], $result['name'], $tmp->fetch_assoc()['type']);
35-
}
36-
else{
37-
$service = new Service($_GET['id'], $result['name']);
27+
// get name, description and status.type (status of service) by id
28+
$query = $mysqli->prepare("select services.id, name, description, status.type from services inner join status on status.id = services.id where services.id = ?;");
29+
$query->bind_param("i", $_GET['id']);
30+
$query->execute();
31+
$result = $query->get_result()->fetch_assoc();
32+
// if type is a number then return it, else just return the service name/desc
33+
if (is_numeric($result["type"])) {
34+
$service = new Service($_GET["id"], $result["name"], $result["description"], '', $result["type"]);
35+
} else {
36+
$service = new Service($_GET["id"], $result["name"], $result["description"]);
3837
}
3938

4039
echo json_encode($service);

0 commit comments

Comments
 (0)