From 53a0e5a19cfcdce4aa86535f7fa7c5fd20813afe Mon Sep 17 00:00:00 2001
From: Muhamad Irfan Maulana <13515037@std.stei.itb.ac.id>
Date: Wed, 11 Apr 2018 11:38:17 +0700
Subject: [PATCH] Add api for greentyno

---
 src/api-greentyno/connect.php      | 14 ++++++++++++++
 src/api-greentyno/getStatus.php    | 27 +++++++++++++++++++++++++++
 src/api-greentyno/updateStatus.php | 18 ++++++++++++++++++
 3 files changed, 59 insertions(+)
 create mode 100644 src/api-greentyno/connect.php
 create mode 100644 src/api-greentyno/getStatus.php
 create mode 100644 src/api-greentyno/updateStatus.php

diff --git a/src/api-greentyno/connect.php b/src/api-greentyno/connect.php
new file mode 100644
index 0000000..8fc174c
--- /dev/null
+++ b/src/api-greentyno/connect.php
@@ -0,0 +1,14 @@
+<?php
+    $servername = "localhost";
+    $username = "root";
+    $password = "";
+    $dbname = "greentyno";
+
+    // Create
+    $conn = mysqli_connect($servername, $username, $password, $dbname);
+
+    // Check
+    if (!$conn) {
+        die("Connection failed: " . mysqli_connect_error());
+    }
+?>
diff --git a/src/api-greentyno/getStatus.php b/src/api-greentyno/getStatus.php
new file mode 100644
index 0000000..3563788
--- /dev/null
+++ b/src/api-greentyno/getStatus.php
@@ -0,0 +1,27 @@
+<?php
+
+    require_once('connect.php');
+
+    $id = $_GET['id'];
+
+    $query = "SELECT * FROM user WHERE id=1";
+    $result = mysqli_query($conn, $query);
+    $ret = array();
+
+    /* Menampilkan hasil dari Query */
+    if (mysqli_num_rows($result) > 0) {
+        while($row = mysqli_fetch_assoc($result)) {
+            $users = array();
+            $users["id"] = $row['id'];
+            $users["email"] = $row['email'];
+            $users["year"] = $row['year'];
+            $users["humidity"] = $row['humidity'];
+            $users["light"] = $row['light'];
+            array_push($ret, $users);
+        }
+    }
+    header('Access-Control-Allow-Origin: *');
+
+    echo json_encode($users);
+
+?>
diff --git a/src/api-greentyno/updateStatus.php b/src/api-greentyno/updateStatus.php
new file mode 100644
index 0000000..73ccfa5
--- /dev/null
+++ b/src/api-greentyno/updateStatus.php
@@ -0,0 +1,18 @@
+<?php
+
+    require_once('connect.php');
+
+    $humidity = $_GET['humidity'];
+    $light = $_GET['light'];
+
+    $query = "UPDATE user SET humidity=" . $humidity . " , light=" . $light . " WHERE id=1";
+    
+    if (mysqli_query($conn, $query)) {
+        echo "Record updated successfully";
+    } else {
+        echo "Error updating record: " . mysqli_error($conn);
+    }
+
+    header('Access-Control-Allow-Origin: *');
+
+?>
-- 
GitLab