Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.74 KB | None | 0 0
  1. <?php
  2. include 'datavalues.php';
  3.  
  4. class DataLayer
  5. {
  6.     private $host = DB_HOST;
  7.     private $user = DB_USER;
  8.     private $pass = DB_PASS;
  9.     private $dbname = DB_NAME;
  10.  
  11.     private $dbh;
  12.     private $error;
  13.  
  14.     private $stmt;
  15.  
  16.     public function __construct()
  17.     {
  18.         $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
  19.         $options = array(
  20.                             PDO::ATTR_PERSISTENT => true,
  21.                             PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  22.                         );
  23.  
  24.         try
  25.         {
  26.                 $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
  27.         }
  28.         catch (PDOException $e)
  29.         {
  30.             $this->error = $e->getMessage();
  31.         }
  32.     }
  33.  
  34.     public function query($query)
  35.     {
  36.  
  37.             $this->stmt = $this->dbh->prepare($query);
  38.  
  39.  
  40.     }
  41.  
  42.     public function bind($param, $value, $type = null)
  43.     {
  44.         if (is_null($type))
  45.         {
  46.             switch (true)
  47.             {
  48.                 case is_int($value):
  49.                     $type = PDO::PARAM_INT;
  50.                     break;
  51.                 case is_bool($value):
  52.                     $type = PDO::PARAM_BOOL;
  53.                     break;
  54.                 case is_null($value):
  55.                     $type = PDO::PARAM_NULL;
  56.                     break;
  57.                 default:
  58.                     $type = PDO::PARAM_STR;
  59.             }
  60.         }
  61.        
  62.         $this->stmt->bindValue($param, $value, $type);
  63.     }
  64.  
  65.     public function execute()
  66.     {
  67.         return $this->stmt->execute();
  68.     }
  69.  
  70.     public function resultset()
  71.     {
  72.         $this->execute();
  73.         return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  74.     }
  75.  
  76.     public function single()
  77.     {
  78.         $this->execute();
  79.         return $this->stmt->fetch(PDO::FETCH_ASSOC);
  80.     }
  81.  
  82.     public function rowCount()
  83.     {
  84.         return $this->stmt->rowCount();
  85.     }
  86.  
  87.     public function lastInsertId()
  88.     {
  89.         return $this->dbh->lastInsertId();
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement