SHARE
TWEET

Untitled

a guest Aug 23rd, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. if (count(get_included_files()) <= 1) {
  3.     exit;
  4. }
  5.  
  6. class database {
  7.     private $host       = DB_HOST;
  8.     private $dbhname    = DB_NAME;
  9.     private $user       = DB_USERNAME;
  10.     private $pass       = DB_PASSWORD;
  11.  
  12.     private $dbh;
  13.     private $stmt;
  14.     private $error;
  15.    
  16.     public function __construct() {
  17.  
  18.         // Set Connection
  19.         $con = 'mysql:host=' . $this->host . ';dbname=' . $this->dbhname;
  20.        
  21.         // Set options
  22.         // If you want to display errors, use the following values
  23.         // true
  24.         // ERRMODE_EXCEPTION
  25.         $options = array(
  26.             PDO::ATTR_PERSISTENT    => false,
  27.             PDO::ATTR_ERRMODE       => PDO::ERRMODE_SILENT
  28.         );
  29.        
  30.         // Create a new PDO instanace
  31.         try {
  32.             $this->dbh = new PDO($con, $this->user, $this->pass, $options);
  33.         } catch(PDOException $e) {
  34.             echo $e->getMessage();
  35.             $this->error = $e->getMessage();
  36.         }
  37.     }
  38.    
  39.     public function query($query) {
  40.         $this->stmt = $this->dbh->prepare($query);
  41.     }
  42.    
  43.     public function bind($param, $value, $type = null) {
  44.         if (is_null($type)) {
  45.             switch (true) {
  46.                 case is_int($value):
  47.                     $type = PDO::PARAM_INT;
  48.                     break;
  49.                 case is_bool($value):
  50.                     $type = PDO::PARAM_BOOL;
  51.                     break;
  52.                 case is_null($value):
  53.                     $type = PDO::PARAM_NULL;
  54.                     break;
  55.                 default:
  56.                     $type = PDO::PARAM_STR;
  57.             }
  58.         }
  59.        
  60.         $this->stmt->bindValue($param, $value, $type);
  61.     }
  62.    
  63.     public function execute() {
  64.         return $this->stmt->execute();
  65.     }
  66.    
  67.     public function resultSet() {
  68.         $this->execute();
  69.         return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  70.     }
  71.    
  72.     public function single() {
  73.         $this->execute();
  74.         return $this->stmt->fetch(PDO::FETCH_ASSOC);
  75.     }
  76.    
  77.     public function rowCount() {
  78.         return $this->stmt->rowCount();
  79.     }
  80.    
  81.     public function lastInsertId() {
  82.         return $this->dbh->lastInsertId();
  83.     }
  84.    
  85.     public function beginTransaction() {
  86.         return $this->dbh->beginTransaction();
  87.     }
  88.    
  89.     public function endTransaction() {
  90.         return $this->dbh->commit();
  91.     }
  92.    
  93.     public function cancelTransaction() {
  94.         return $this->dbh->rollBack();
  95.     }
  96.    
  97.     public function debugDumpParams() {
  98.         return $this->stmt->debugDumpParams();
  99.     }
  100. }
  101. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top