Advertisement
cafreak

PDO Wrapper

Nov 9th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.05 KB | None | 0 0
  1. <?php
  2.     class Database{
  3.         private $db;
  4.        
  5.         public function __construct($config){
  6.             $driver = strtolower($config['database']['driver']);
  7.             $host = $config['database']['host'];
  8.             $database = $config['database']['name'];
  9.             $username = $config['database']['username'];
  10.             $password = $config['database']['password'];
  11.            
  12.             $this->connect($driver, $host, $database, $username, $password);
  13.         }
  14.        
  15.         public function connect($driver, $host, $database, $username, $password){
  16.             try{
  17.                 $this->db = new PDO($driver.":host=".$host.";dbname=".$database, $username, $password);
  18.                 $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  19.                 $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  20.             }catch(PDOException $ex){
  21.                 $errorCode = $ex->getCode();
  22.                 $errorMessage = $ex->getMessage();
  23.                 $errorFile = $ex->getFile();
  24.                 $errorLine = $ex->getLine();
  25.                 include(dirname(dirname(__FILE__))."/error.php");
  26.             }
  27.         }
  28.        
  29.         public function nonQuery($query){
  30.             try{
  31.                 $run = $this->db->prepare($query);
  32.                 if(func_get_args() > 0){
  33.                     $args = func_get_args();
  34.                     array_shift($args);
  35.                     return $run->execute($args);
  36.                 }else{
  37.                     return $run->execute();
  38.                 }
  39.             }catch(PDOException $ex){
  40.                 $errorCode = $ex->getCode();
  41.                 $errorMessage = $ex->getMessage();
  42.                 $errorFile = $ex->getFile();
  43.                 $errorLine = $ex->getLine();
  44.                 include(dirname(dirname(__FILE__))."/error.php");
  45.                
  46.             }
  47.         }
  48.        
  49.         function query($query){
  50.             try{
  51.                 $run = $this->db->prepare($query);
  52.                 if(func_get_args() > 0){
  53.                     $args = func_get_args();
  54.                     array_shift($args);
  55.                     $run->execute($args);
  56.                 }else{
  57.                     $run->execute();
  58.                 }  
  59.                 return $run; // using: ->fetchall(PDO::FETCH_ASSOC); needs: print_r($db->query("SELECT * FROM `test2`"));
  60.             }catch(PDOException $ex){
  61.                 $errorCode = $ex->getCode();
  62.                 $errorMessage = $ex->getMessage();
  63.                 $errorFile = $ex->getFile();
  64.                 $errorLine = $ex->getLine();
  65.                 include(dirname(dirname(__FILE__))."/error.php");
  66.             }
  67.         }
  68.     }
  69.  
  70.     $db = new Database($config);
  71. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement