Advertisement
valneyf

database.php

Feb 22nd, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.02 KB | None | 0 0
  1. <?php
  2. mysqli_report(MYSQLI_REPORT_STRICT);
  3. function open_database() {
  4.     try {
  5.         $conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  6.         return $conn;
  7.     } catch (Exception $e) {
  8.         echo $e->getMessage();
  9.         return null;
  10.     }
  11. }
  12. function close_database($conn) {
  13.     try {
  14.         mysqli_close($conn);
  15.     } catch (Exception $e) {
  16.         echo $e->getMessage();
  17.     }
  18. }
  19.  
  20. /**
  21.  *  Pesquisa um Registro pelo ID em uma Tabela
  22.  */
  23. function find( $table = null, $id = null ) {
  24.  
  25.     $database = open_database();
  26.     $found = null;
  27.  
  28.     try {
  29.       if ($id) {
  30.         $sql = "SELECT * FROM " . $table . " WHERE id = " . $id;
  31.         $result = $database->query($sql);
  32.        
  33.         if ($result->num_rows > 0) {
  34.           $found = $result->fetch_assoc();
  35.         }
  36.        
  37.       } else {
  38.        
  39.         $sql = "SELECT * FROM " . $table;
  40.         $result = $database->query($sql);
  41.        
  42.         if ($result->num_rows > 0) {
  43.           $found = $result->fetch_all(MYSQLI_ASSOC);
  44.        
  45.         /* Metodo alternativo
  46.         $found = array();
  47.         while ($row = $result->fetch_assoc()) {
  48.           array_push($found, $row);
  49.         } */
  50.         }
  51.       }
  52.     } catch (Exception $e) {
  53.       $_SESSION['message'] = $e->GetMessage();
  54.       $_SESSION['type'] = 'danger';
  55.   }
  56.    
  57.     close_database($database);
  58.     return $found;
  59. }
  60.  
  61. /**
  62.  *  Pesquisa Todos os Registros de uma Tabela
  63.  */
  64. function find_all( $table ) {
  65.   return find($table);
  66. }
  67.  
  68. /**
  69. *  Insere um registro no BD
  70. */
  71. function save($table = null, $data = null) {
  72.   $database = open_database();
  73.  
  74.   $columns = null;
  75.   $values = null;
  76.  
  77.   //print_r($data);
  78.  
  79.   foreach ($data as $key => $value) {
  80.     $columns .= trim($key, "'") . ",";
  81.     $values .= "'$value',";
  82.   }
  83.   // remove a ultima virgula
  84.   $columns = rtrim($columns, ',');
  85.   $values = rtrim($values, ',');
  86.  
  87.   $sql = "INSERT INTO " . $table . "($columns)" . " VALUES " . "($values);";
  88.  
  89.   try {
  90.     $database->query($sql);
  91.  
  92.     $_SESSION['message'] = 'Registro cadastrado com sucesso.';
  93.     $_SESSION['type'] = 'success';
  94.  
  95.   } catch (Exception $e) {
  96.  
  97.     $_SESSION['message'] = 'Nao foi possivel realizar a operacao.';
  98.     $_SESSION['type'] = 'danger';
  99.   }
  100.   close_database($database);
  101. }
  102.  
  103. /**
  104.  *  Atualizacao/Edicao de Cliente
  105.  */
  106. function edit() {
  107.  
  108.   $now = date_create('now', new DateTimeZone('America/Sao_Paulo'));
  109.  
  110.   if (isset($_GET['id'])) {
  111.  
  112.     $id = $_GET['id'];
  113.  
  114.     if (isset($_POST['customer'])) {
  115.  
  116.       $customer = $_POST['customer'];
  117.       $customer['modified'] = $now->format("Y-m-d H:i:s");
  118.  
  119.       update('customers', $id, $customer);
  120.       header('location: index.php');
  121.     } else {
  122.  
  123.       global $customer;
  124.       $customer = find('customers', $id);
  125.     }
  126.   } else {
  127.     header('location: index.php');
  128.   }
  129. }
  130.  
  131. /**
  132.  *  Atualiza um registro em uma tabela, por ID
  133.  */
  134. function update($table = null, $id = 0, $data = null) {
  135.  
  136.   $database = open_database();
  137.  
  138.   $items = null;
  139.  
  140.   foreach ($data as $key => $value) {
  141.     $items .= trim($key, "'") . "='$value',";
  142.   }
  143.  
  144.   // remove a ultima virgula
  145.   $items = rtrim($items, ',');
  146.  
  147.   $sql  = "UPDATE " . $table;
  148.   $sql .= " SET $items";
  149.   $sql .= " WHERE id=" . $id . ";";
  150.  
  151.   try {
  152.     $database->query($sql);
  153.  
  154.     $_SESSION['message'] = 'Registro atualizado com sucesso.';
  155.     $_SESSION['type'] = 'success';
  156.  
  157.   } catch (Exception $e) {
  158.  
  159.     $_SESSION['message'] = 'Nao foi possivel realizar a operacao.';
  160.     $_SESSION['type'] = 'danger';
  161.   }
  162.   close_database($database);
  163. }
  164.  
  165. /**
  166.  *  Remove uma linha de uma tabela pelo ID do registro
  167.  */
  168. function remove( $table = null, $id = null ) {
  169.   $database = open_database();
  170.    
  171.   try {
  172.     if ($id) {
  173.       $sql = "DELETE FROM " . $table . " WHERE id = " . $id;
  174.       $result = $database->query($sql);
  175.       if ($result = $database->query($sql)) {      
  176.         $_SESSION['message'] = "Registro Removido com Sucesso.";
  177.         $_SESSION['type'] = 'success';
  178.       }
  179.     }
  180.   } catch (Exception $e) {
  181.     $_SESSION['message'] = $e->GetMessage();
  182.     $_SESSION['type'] = 'danger';
  183.   }
  184.   close_database($database);
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement