Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
1.2 kB
1
Indexable
Never
<?php
header('Content-Type: application/json');
$servername = "localhost";
$username = "root";
$password = "";
$database_name = "versiune0";
 
$conn = new mysqli($servername, $username, $password, $database_name);
if ($conn->connect_error) {
    echo json_encode(['error' => "Connection failed: " . $conn->connect_error]);
    exit;
}
 
// Building the SQL query dynamically based on GET parameters
$queryParts = [];
if (isset($_GET['temperature'])) {
    $queryParts[] = "temp = " . intval($_GET['temperature']);
}
if (isset($_GET['humidity'])) {
    $queryParts[] = "humi = " . intval($_GET['humidity']);
}
if (isset($_GET['pressure'])) {
    $queryParts[] = "press = " . intval($_GET['pressure']);
}
// Add other parameters as needed
 
$whereClause = "";
if (!empty($queryParts)) {
    $whereClause = " WHERE " . join(" AND ", $queryParts);
}
 
$sql = "SELECT id, temp, humi, press, CO, dist, butt, posi FROM tabel" . $whereClause;
$result = $conn->query($sql);
 
$data = [];
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $data[] = $row;
    }
}
echo json_encode($data);
 
$conn->close();
?>
Leave a Comment