Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.88 KB | None | 0 0
  1. <?php
  2.  
  3. class SQLObject{
  4.     private $_dbLink = NULL;
  5.     private $_dbHost = NULL;
  6.     private $_dbName = NULL;
  7.     private $_dbUser = NULL;
  8.     private $_dbPass = NULL;
  9.  
  10.     public function __construct($host, $name, $user, $pass){
  11.         $this->_dbHost = $host;
  12.         $this->_dbName = $name;
  13.         $this->_dbUser = $user;
  14.         $this->_dbPass = $pass;
  15.         if(!$this->_openConnection())
  16.             throw new Exception('La connexion a la base de données a échoué.');
  17.     }
  18.  
  19.     public function getLink(){
  20.         if(!is_null($this->_dbLink) OR $this->openConnection())
  21.             return $this->_dbLink;
  22.         else
  23.             return false;
  24.     }
  25.  
  26.     private function _openConnection(){
  27.         try{
  28.             $dsn = 'mysql:dbname=' . $this->_dbName . ';host=' . $this->_dbHost;
  29.             $this->_dbLink = new PDO($dsn, $this->_dbUser, $this->_dbPass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ));
  30.             return true;
  31.         }
  32.         catch(Exception $e){
  33.             return false;
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement