This repository was archived by the owner on Jul 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesp-database.php
More file actions
161 lines (136 loc) · 4.5 KB
/
Copy pathesp-database.php
File metadata and controls
161 lines (136 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
$servername = getenv('MYSQL_SERVER');
$dbname = getenv('MYSQL_DATABASE');
$username = getenv('MYSQL_USER');
$password = getenv('MYSQL_PASSWORD');
function insertReading($sensor, $location, $value1, $value2, $table = null)
{
global $servername, $username, $password, $dbname;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Use the table if no table is provided
if (empty($table)) {
$table = 'SensorData';
}
$sql = "INSERT INTO $table (sensor, location, value1, value2) VALUES ('" . $sensor . "', '" . $location . "', '" . $value1 . "', '" . $value2 . "')";
if ($conn->query($sql) === TRUE) {
return "New record created successfully";
} else {
return "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
function getAllReadings($limit, $table)
{
global $servername, $username, $password, $dbname;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, sensor, location, value1, value2, reading_time FROM $table ORDER BY reading_time DESC LIMIT " . $limit;
if ($result = $conn->query($sql)) {
return $result;
} else {
return false;
}
$conn->close();
}
function getLastReadings($table)
{
global $servername, $username, $password, $dbname;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, sensor, location, value1, value2, reading_time FROM $table ORDER BY reading_time DESC LIMIT 1";
if ($result = $conn->query($sql)) {
return $result->fetch_assoc();
} else {
return false;
}
$conn->close();
}
function minReading($limit, $value, $table)
{
global $servername, $username, $password, $dbname;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT MIN($value) AS min_amount FROM (SELECT $value FROM $table ORDER BY reading_time DESC LIMIT $limit) AS min";
if ($result = $conn->query($sql)) {
return $result->fetch_assoc();
} else {
return false;
}
$conn->close();
}
function maxReading($limit, $value, $table)
{
global $servername, $username, $password, $dbname;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT MAX($value) AS max_amount FROM (SELECT $value FROM $table ORDER BY reading_time DESC LIMIT $limit) AS max";
if ($result = $conn->query($sql)) {
return $result->fetch_assoc();
} else {
return false;
}
$conn->close();
}
function avgReading($limit, $value, $table)
{
global $servername, $username, $password, $dbname;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT AVG($value) AS avg_amount FROM (SELECT $value FROM $table ORDER BY reading_time DESC LIMIT $limit) AS avg";
if ($result = $conn->query($sql)) {
return $result->fetch_assoc();
} else {
return false;
}
$conn->close();
}
function getAvailableTables()
{
global $servername, $username, $password, $dbname;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$tablePrefix = 'SensorData';
$sql = "SHOW TABLES LIKE '$tablePrefix%'";
$result = $conn->query($sql);
$tables = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_row()) {
$tables[] = $row[0];
}
}
// Add the main table if it's not already in the array
if (!in_array($tablePrefix, $tables)) {
$tables[] = $tablePrefix;
}
$conn->close();
return $tables;
}