Advertisement
cafreak

mysql_database_pdo_class.php

Sep 25th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.23 KB | None | 0 0
  1. <?php
  2.     $db = new Database();
  3.     $db->setVariables();
  4.  
  5.     class Database{
  6.         private $db;
  7.        
  8.         private $host;
  9.         private $database;
  10.         private $username;
  11.         private $password;
  12.        
  13.         function setVariables(){
  14.             global $config;
  15.             $this->host = $config['server']['host'];
  16.             $this->database = $config['server']['name'];
  17.             $this->username = $config['master']['username'];
  18.             $this->password = $config['master']['password'];
  19.            
  20.             $this->connect();
  21.         }
  22.        
  23.         function connect(){
  24.             try{
  25.                 $this->db = new PDO("mysql:host=1".$this->host.";dbname=".$this->database, $this->username, $this->password);
  26.                 $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  27.                 $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  28.             }catch(PDOException $ex){
  29.                 $errorFile = file_get_contents(dirname(__FILE__)."/error.php");
  30.                 $errorFile = str_replace("%ErrorCode%", $ex->getCode(), $errorFile);
  31.                 $errorFile = str_replace("%ErrorMessage%", $ex->getMessage(), $errorFile);
  32.                 die($errorFile);
  33.             }
  34.         }
  35.        
  36.         function nonQuery($query){
  37.             try{
  38.                 $run = $this->db->prepare($query);
  39.                 if(func_get_args() > 0){
  40.                     $args = func_get_args();
  41.                     array_shift($args);
  42.                     if($run->execute($args)){
  43.                         return true;
  44.                     }else{
  45.                         return false;
  46.                     }
  47.                 }else{
  48.                     if($run->execute()){
  49.                         return true;
  50.                     }else{
  51.                         return false;
  52.                     }
  53.                 }
  54.             }catch(PDOException $ex){
  55.                 $errorFile = file_get_contents(dirname(__FILE__)."/error.php");
  56.                 $errorFile = str_replace("%ErrorCode%", $ex->getCode(), $errorFile);
  57.                 $errorFile = str_replace("%ErrorMessage%", $ex->getMessage(), $errorFile);
  58.                 die($errorFile);
  59.             }
  60.         }
  61.        
  62.         function query($query){
  63.             try{
  64.                 $run = $this->db->prepare($query);
  65.                 if(func_get_args > 0){
  66.                     $args = func_get_args();
  67.                     $args = array_shift($args);
  68.                     $run->execute($args);
  69.                 }else{
  70.                     $run->execute();
  71.                 }  
  72.                 return $run->fetchall(PDO::FETC_ASSOC);
  73.             }catch(PDOException $ex){
  74.                 $errorFile = file_get_contents(dirname(__FILE__)."/error.php");
  75.                 $errorFile = str_replace("%ErrorCode%", $ex->getCode(), $errorFile);
  76.                 $errorFile = str_replace("%ErrorMessage%", $ex->getMessage(), $errorFile);
  77.                 die($errorFile);
  78.             }
  79.         }
  80.     }
  81.  
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement