Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $method = $_SERVER['REQUEST_METHOD'];
- $request = explode('/', trim($_SERVER['PATH_INFO'],'/'));
- $input = json_decode(file_get_contents('php://input'),true);
- // konektor mysql
- $link = mysqli_connect('localhost', 'root', '', 'iot_bawang');
- mysqli_set_charset($link,'utf8');
- // database set
- $nama_tabel = "tanah_lembab";
- $field_tb = "id_lembab";
- $order = "tgl_update";
- $data = preg_replace('/[^a-z0-9_]+/i','',array_shift($request));
- $id = array_shift($request)+0;
- if (strcmp($data, 'data') ==0) {
- switch ($method)
- {
- case 'GET':
- $sql = "select * from ".$nama_tabel.($id?" WHERE ".$field_tb."=$id":'')." order by ". $order. " DESC LIMIT 1"; break;
- }
- $result = mysqli_query($link,$sql);
- if (!$result) {
- http_response_code(404);
- die(mysqli_error());
- }
- if ($method == 'GET') {
- $hasil=array();
- while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
- {
- $hasil[]=$row;
- }
- echo json_encode($hasil);
- } elseif ($method == 'POST') {
- echo mysqli_insert_id($link);
- } else {
- echo mysqli_affected_rows($link);
- }
- }else{
- $hasil1 = array('status' => false, 'message' => 'Access Denied');
- echo json_encode($hasil1);
- }
- mysqli_close($link);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement