Untitled

mail@pastecode.io avatar
unknown
php
a month ago
1.1 kB
2
Indexable
Never
<?php
include "../inc/dbinfo.inc";
header('Content-Type: application/json');

$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
if (mysqli_connect_errno()) {
    echo json_encode(["error" => "Failed to connect to MySQL: " . mysqli_connect_error()]);
    exit;
}

$database = mysqli_select_db($connection, DB_DATABASE);
if (!$database) {
    echo json_encode(["error" => "Failed to select database"]);
    exit;
}

$query = $connection->prepare("SELECT id, last_update FROM sessions ORDER BY last_update DESC LIMIT 1");
$query->execute();
$result = $query->get_result();
if ($row = $result->fetch_assoc()) {
    $session_id = $row["id"];

    $query = $connection->prepare("SELECT measure_time, temperature FROM measurements WHERE session_id = ? ORDER BY measure_time DESC LIMIT 100");
    $query->bind_param("i", $session_id);
    $query->execute();
    $result = $query->get_result();

    $data = [];
    while ($row = $result->fetch_assoc()) {
        $data[] = $row;
    }

    echo json_encode($data);
} else {
    echo json_encode(["error" => "No session found"]);
}
?>
Leave a Comment