Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.59 KB | None | 0 0
  1. class DBConnection {
  2.  
  3.     public $baza;
  4.     public $user;
  5.     public $pass;
  6.     public $host;
  7.     public $expr;
  8.  
  9.     public function __construct() {
  10.         $this->baza = 'press';
  11.         $this->user = 'root';
  12.         $this->pass = '';
  13.         $this->host = 'localhost';
  14.     }
  15.  
  16.     public function connect() {
  17.         try {
  18.             $str = "mysql:host=$this->host;dbname=$this->baza";
  19.             $this->expr = new PDO($str, $this->user, $this->pass);
  20.         } catch (PDOException $ex) {
  21.             echo "GRESKA SA KONEKCIJOM : " . $ex()->getMessage();
  22.         }
  23.     }
  24.  
  25.     public function __destruct() {
  26.         $this->expr = null;
  27.     }
  28.  
  29.     public function query($sql) {
  30.         try {
  31.             $rez = $this->expr->query($sql);
  32.             $niz = $rez->fetchAll(PDO::FETCH_ASSOC);
  33.             return $niz;
  34.         } catch (PDOException $ex) {
  35.             echo "GRESKA SA UPITOM: " . $ex()->getMessage();
  36.         }
  37.     }
  38.  
  39.     public function insert($sql) {
  40.         try {
  41.             $this->expr->beginTransaction();
  42.             $this->expr->exec($sql);
  43.             $this->expr->commit();
  44.         } catch (PDOException $ex) {
  45.             $this->expr->rollback();
  46.             echo "GRESKA INSERT: " . $ex()->getMessage();
  47.         }
  48.     }
  49.  
  50.     public function delete($sql) {
  51.         try {
  52.             $this->expr->beginTransaction();
  53.             $this->expr->exec($sql);
  54.             $this->expr->commit();
  55.         } catch (PDOException $ex) {
  56.             $this->expr->rollback();
  57.             echo "GRESKA DELETE: " . $ex()->getMessage();
  58.         }
  59.     }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement