Advertisement
gayanov

Untitled

Jun 30th, 2017
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.98 KB | None | 0 0
  1. <?php
  2.  
  3. class Database{
  4.     public $isConn;
  5.     protected $datab;
  6.    
  7.     // connect to db
  8.     public function __construct($username = "root", $password = "", $host = "localhost", $dbname = "myproject", $options = []){
  9.         $this->isConn = TRUE;
  10.         try {
  11.             $this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
  12.             $this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  13.             $this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  14.         } catch (PDOException $e) {
  15.             throw new Exception($e->getMessage());
  16.         }
  17.        
  18.     }
  19.    
  20.     // disconnect from db
  21.     public function Disconnect(){
  22.         $this->datab = NULL;
  23.         $this->isConn = FALSE;
  24.     }
  25.     // get row
  26.     public function getRow($query, $params = []){
  27.         try {
  28.             $stmt = $this->datab->prepare($query);
  29.             $stmt->execute($params);
  30.             return $stmt->fetch();
  31.         } catch (PDOException $e) {
  32.             throw new Exception($e->getMessage());
  33.         }
  34.     }
  35.     // get rows
  36.     public function getRows($query, $params = []){
  37.         try {
  38.             $stmt = $this->datab->prepare($query);
  39.             $stmt->execute($params);
  40.             return $stmt->fetchAll();
  41.         } catch (PDOException $e) {
  42.             throw new Exception($e->getMessage());
  43.         }
  44.     }
  45.     // insert row
  46.     public function insertRow($query, $params = []){
  47.         try {
  48.             $stmt = $this->datab->prepare($query);
  49.             $stmt->execute($params);
  50.             return TRUE;
  51.         } catch (PDOException $e) {
  52.             throw new Exception($e->getMessage());
  53.         }
  54.     }
  55.     // update row
  56.     public function updateRow($query, $params = []){
  57.         $this->insertRow($query, $params);
  58.     }
  59.     // delete row
  60.     public function deleteRow($query, $params = []){
  61.         $this->insertRow($query, $params);
  62.     }
  63. }
  64.  
  65. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement