Advertisement
AbdulMuttaqin

Untitled

Jul 26th, 2021
1,247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.91 KB | None | 0 0
  1. <?php
  2.  
  3. $conn = mysqli_connect("localhost", "root", "", "ta_nisa");
  4.  
  5. if (mysqli_connect_errno()) {
  6.     echo "Gagal terhubung ke MySQL: " . mysqli_connect_error();
  7.     exit();
  8. }
  9.  
  10. /*
  11.     fungsi untuk mengambil semua data dari tabel
  12.     cara menggunakan: get_all("nama_tabel")
  13.     contoh: $bandara = get_all("bandara")
  14. */
  15. function get_all($query)
  16. {
  17.     global $conn;
  18.     $result = mysqli_query($conn, $query);
  19.     $rows = [];
  20.     while ($row = mysqli_fetch_assoc($result)) {
  21.         $rows[] = $row;
  22.     }
  23.     return $rows;
  24. }
  25.  
  26. function get_where($table, $where)
  27. {
  28.     global $conn;
  29.     $result = mysqli_query($conn, "SELECT * FROM $table WHERE $where");
  30.    
  31.     if (!$result) {
  32.         die('Query Error : '. mysqli_errno($conn) . ' - ' . mysqli_error($conn));
  33.     }
  34.  
  35.     return mysqli_fetch_assoc($result);
  36. }
  37.  
  38. function generate_random_string($length = 10)
  39. {
  40.     $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  41.     $charactersLength = strlen($characters);
  42.     $randomString = '';
  43.     for ($i = 0; $i < $length; $i++) {
  44.         $randomString .= $characters[rand(0, $charactersLength - 1)];
  45.     }
  46.     return $randomString;
  47. }
  48.  
  49. function upload_file($file)
  50. {
  51.     $target_dir = "img/";
  52.     $file_name = generate_random_string($length = 5) . '-' .basename($_FILES["file"]['name']);
  53.     $target_file = $target_dir . $file_name;
  54.     move_uploaded_file($_FILES["file"]['tmp_name'], "img/".$_FILES["file"]['name']);
  55.     //echo $target_file;
  56.     return $file_name;
  57. }
  58. function insert($table_name = "", $data = [])
  59. {
  60.     global $conn;
  61.     $fields =  "";
  62.     $values = "";
  63.  
  64.     foreach ($data as $key => $value) {
  65.         if ($key === array_key_last($data)) {
  66.             $fields .= "$key";
  67.             $values .= "'$value'";
  68.         } else {
  69.             $fields .= "$key, ";
  70.             $values .= "'$value', ";
  71.         }
  72.     }
  73.  
  74.  
  75.     $query = "INSERT INTO $table_name ( $fields )
  76.              VALUES ( $values );";
  77.  
  78.     $result = mysqli_query($conn, $query);
  79.  
  80.     if (!$result) {
  81.         die('Query Error : '. mysqli_errno($conn) . ' - ' . mysqli_error($conn));
  82.     }
  83. }
  84.  
  85. function update($table_name = "", $data = [], $where)
  86. {
  87.     global $conn;
  88.     $update_field = "";
  89.  
  90.     foreach ($data as $key => $value) {
  91.         if ($key === array_key_last($data)) {
  92.             $update_field .= "$key = '$value'";
  93.         } else {
  94.             $update_field .= "$key = '$value', ";
  95.         }
  96.     }
  97.  
  98.  
  99.     $query = "UPDATE $table_name SET $update_field
  100.              WHERE $where;";
  101.  
  102.     $result = mysqli_query($conn, $query);
  103.  
  104.     if (!$result) {
  105.         die('Query Error : '. mysqli_errno($conn) . ' - ' . mysqli_error($conn));
  106.     }
  107. }
  108.  
  109. function delete($table, $where) {
  110.     global $conn;
  111.     $result = mysqli_query($conn, "DELETE FROM $table WHERE $where");
  112.     if (!$result) {
  113.         die('Query Error : '. mysqli_errno($conn) . ' - ' . mysqli_error($conn));
  114.     }
  115. }
  116.  
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement