-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectTest.php
More file actions
51 lines (42 loc) · 1.06 KB
/
connectTest.php
File metadata and controls
51 lines (42 loc) · 1.06 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
<?php
header("Content-type:application/json");
//Data model class
class Relay {
var $id;
var $fans;
function __construct($id, $fans) {
$this->id = $id;
$this->fans = $fans;
}
}
//Connection properties
$servername = "localhost";
$username = "root";
$password = "admin";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Run query
$sqlQuery = "SELECT * FROM db_classroom_management.tbl_relays";
$result = mysqli_query($conn, $sqlQuery);
//Handle response
$numberOfRows = mysqli_num_rows($result);
$response = array();
$counter = 0;
if ($numberOfRows > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$myObj = new Relay($row["relay_id"],$row["no_of_associated_fans"]);
$response[$counter] = ($myObj);
$counter++;
}
} else {
echo json_encode("0 results");
}
//Finally sending response
$jsonResponse->answers = $response;
echo json_encode($jsonResponse);
?>