Webservice sederhana untuk keperluan pembelajaran pemrograman webservice di ITB STIKOM Bali, MK Perancangan Web dan Pemrograman Web.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.2 KiB

  1. <?php
  2. session_start();
  3. define(DBHOST, '127.0.0.1');
  4. define(DBUSER, 'dbuser');
  5. define(DBPASS, 'dbpass');
  6. define(DBNAME, 'dbname');
  7. if(!$link = @mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME))
  8. die(mysqli_connect_error());
  9. function db_query($sql) {
  10. global $link;
  11. if(!$result=@mysqli_query($link, $sql))
  12. die(mysqli_error($link));
  13. return $result;
  14. }
  15. function get_all($table, $sort_by=false, $ascending=true, $where=false) {
  16. $sql = "SELECT * FROM $table";
  17. if($where) $sql .= " WHERE $where";
  18. if($sort_by) {
  19. $sql .= " ORDER BY $sort_by";
  20. if($ascending) $sql .= " ASC";
  21. else $sql .= " DESC";
  22. }
  23. $result = db_query($sql);
  24. $return = [];
  25. while($row=mysqli_fetch_assoc($result)) $return[] = $row;
  26. return $return;
  27. }
  28. function get_one($table, $where) {
  29. $sql = "SELECT * FROM $table WHERE $where";
  30. $result = db_query($sql);
  31. if($row=mysqli_fetch_assoc($result)) return $row;
  32. else return [];
  33. }
  34. function resp_success($array) {
  35. resp_header();
  36. echo json_encode(['status'=>1, 'data'=>$array]);
  37. }
  38. function resp_failed() {
  39. resp_header();
  40. echo json_encode(['status'=>0]);
  41. }
  42. function resp_header()
  43. {
  44. header('Access-Control-Allow-Origin: *');
  45. header('Content-Type: application/json; charset=UTF-8');
  46. }