Advertisement
CosminVarlan

BD_in_PHP

Mar 27th, 2020
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.69 KB | None | 0 0
  1. <?php
  2.     class BD{
  3.         private static $conexiune_bd = NULL;
  4.         public static function obtine_conexiune(){
  5.             if (is_null(self::$conexiune_bd))
  6.             {
  7.                 self::$conexiune_bd = new PDO('mysql:host=localhost;dbname=sezatoare', 'cosmin', 'cosmin');
  8.             }
  9.             return self::$conexiune_bd;
  10.         }    
  11.     }
  12.  
  13.     class Actiune{
  14.         public function adaugaMesaj($utilizator, $mesaj){ //create
  15.             $sql = "INSERT INTO mesaje (utilizator, mesaj, created_at, updated_at) VALUES (:utilizator, :mesaj, :created_at, :updated_at)";
  16.             $cerere = BD::obtine_conexiune()->prepare($sql);
  17.             return $cerere -> execute ([
  18.                 'utilizator' => $utilizator,
  19.                 'mesaj' => $mesaj,
  20.                 'created_at' => date('Y-m-d H:i:s'),
  21.                 'updated_at' => date('Y-m-d H:i:s')
  22.             ]);
  23.         }
  24.         public function obtineMesaje(){ // read
  25.             $sql = "SELECT * FROM mesaje";
  26.             $cerere = BD::obtine_conexiune()->prepare($sql);
  27.             $cerere->execute();
  28.             return $cerere->fetchAll();
  29.         }
  30.         public function modificaMesaj($id, $mesaj){  // update
  31.             $sql = "UPDATE mesaje SET mesaj = :mesaj, updated_at = :updated_at WHERE id = :id";
  32.             $cerere = BD::obtine_conexiune()->prepare($sql);
  33.             return $cerere->execute([
  34.                 'mesaj' => $mesaj,
  35.                 'updated_at' => date('Y-m-d H:i:s'),
  36.                 'id' => $id
  37.             ]);
  38.         }
  39.  
  40.         public function stergeMesaj($id){
  41.             $sql = "DELETE FROM mesaje WHERE id = ?";
  42.             $cerere = BD::obtine_conexiune()->prepare($sql);
  43.             return $cerere-> execute([$id]);
  44.         }
  45.     }
  46.  
  47.  
  48.  
  49.     $actiune = new Actiune();
  50.     echo "<h1>Inainte de Inserare:</h1><br />";
  51.     echo "<pre>";
  52.     print_r ($actiune -> obtineMesaje());
  53.     echo "</pre>";
  54.  
  55.        
  56.     $actiune -> adaugaMesaj('Nicolae','Mesaj initial, ce va fi modificat.');
  57.     echo "<h1>Dupa inserare:</h1><br />";
  58.     echo "<pre>";
  59.     $mesaje = $actiune -> obtineMesaje();
  60.     print_r ($mesaje);
  61.     echo "</pre>";
  62.  
  63.     $id_mesaj_inserat = $mesaje[count($mesaje) -1]["id"];
  64.    
  65.     $actiune -> modificaMesaj($id_mesaj_inserat,'Acesta este mesajul modificat');
  66.    
  67.     echo "<h1>Dupa modificare:</h1><br />";
  68.     echo "<pre>";
  69.     $mesaje = $actiune -> obtineMesaje();
  70.     print_r ($mesaje);
  71.     echo "</pre>";    
  72.    
  73.    
  74.     $actiune -> stergeMesaj($id_mesaj_inserat);
  75.    
  76.     echo "<h1>Dupa Stergere:</h1><br />";
  77.     echo "<pre>";
  78.     $mesaje = $actiune -> obtineMesaje();
  79.     print_r ($mesaje);
  80.     echo "</pre>";      
  81.          
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement