Advertisement
Guest User

Untitled

a guest
Mar 29th, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.31 KB | None | 0 0
  1. <?php
  2. /*
  3. *  PDO DATABASE CLASS
  4. *  Connects Database Using PDO
  5.  *  Creates Prepeared Statements
  6.  *  Binds params to values
  7.  *  Returns rows and results
  8. */
  9. class Database {
  10.     private $host = DB_HOST;
  11.     private $user = DB_USER;
  12.     private $pass = DB_PASS;
  13.     private $dbname = DB_NAME;
  14.  
  15.     private $dbh;
  16.     private $error;
  17.     private $stmt;
  18.  
  19.     public function __construct() {
  20.         // Set DSN
  21.         $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
  22.         $options = array (
  23.             PDO::ATTR_PERSISTENT => true,
  24.             PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  25.         );
  26.         // Create a new PDO instanace
  27.         try {
  28.             $this->dbh = new PDO ($dsn, $this->user, $this->pass, $options);
  29.         }       // Catch any errors
  30.         catch ( PDOException $e ) {
  31.             $this->error = $e->getMessage();
  32.         }
  33.     }
  34.  
  35.     // Prepare statement with query
  36.     public function query($query) {
  37.         $this->stmt = $this->dbh->prepare($query);
  38.     }
  39.  
  40.     // Bind values
  41.     public function bind($param, $value, $type = null) {
  42.         if (is_null ($type)) {
  43.             switch (true) {
  44.                 case is_int ($value) :
  45.                     $type = PDO::PARAM_INT;
  46.                     break;
  47.                 case is_bool ($value) :
  48.                     $type = PDO::PARAM_BOOL;
  49.                     break;
  50.                 case is_null ($value) :
  51.                     $type = PDO::PARAM_NULL;
  52.                     break;
  53.                 default :
  54.                     $type = PDO::PARAM_STR;
  55.             }
  56.         }
  57.         $this->stmt->bindValue($param, $value, $type);
  58.     }
  59.  
  60.     // Execute the prepared statement
  61.     public function execute(){
  62.         return $this->stmt->execute();
  63.     }
  64.  
  65.     // Get result set as array of objects
  66.     public function resultset(){
  67.         $this->execute();
  68.         return $this->stmt->fetchAll(PDO::FETCH_OBJ);
  69.     }
  70.  
  71.     // Get single record as object
  72.     public function single(){
  73.         $this->execute();
  74.         return $this->stmt->fetch(PDO::FETCH_OBJ);
  75.     }
  76.  
  77.     // Get record row count
  78.     public function rowCount(){
  79.         return $this->stmt->rowCount();
  80.     }
  81.  
  82.     // Returns the last inserted ID
  83.     public function lastInsertId(){
  84.         return $this->dbh->lastInsertId();
  85.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement