Advertisement
Guest User

Untitled

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