fabi0

Untitled

May 26th, 2014
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.15 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Description of Database
  5.  *
  6.  * @author Ivaylo Ivanov
  7.  */
  8.  
  9. namespace Models;
  10.  
  11. class Database {
  12.  
  13.     private $_pdo,
  14.             $_query,
  15.             $_error = false,
  16.             $_result,
  17.             $_count = 0;
  18.  
  19.     /**
  20.      * Database params.
  21.      *
  22.      * @param type $host
  23.      * @param type $dbname
  24.      * @param type $username
  25.      * @param type $password
  26.      * @param type $charset
  27.      */
  28.     public function __construct($host, $dbname, $username, $password, $charset = 'utf8') {
  29.         try {
  30.             $this->_pdo = new \PDO("mysql:host=$host;dbname=$dbname;charset=$charset", $username, $password);
  31.         } catch (\PDOException $e) {
  32.             die($e->getMessage());
  33.         }
  34.     }
  35.  
  36.     /**
  37.      * <code>
  38.      * $params['sql'] = "SELECT * .....";
  39.      * $params['bind'][':user_name']
  40.      * $params['type']
  41.      * </code>
  42.      * @param type $sql
  43.      * @param type $params
  44.      * @param type $type
  45.      * @return \Database
  46.      */
  47.     public function query($sql, $params = array(), $type = 5) {
  48.         $this->_error = FALSE;
  49.         if ($this->_query = $this->_pdo->prepare($sql)) {
  50.             if (count($params)) {
  51.                 foreach ($params as $field => $values) {
  52.                     $this->_query->bindParam($field, $values['param'], $values['type']);
  53.                 }
  54.             }
  55.  
  56.             if ($this->_query->execute()) {
  57.                 $this->_result = $this->_query->fetchAll($type);
  58.                 $this->_count = $this->_query->rowCount();
  59.             } else {
  60.                 $this->_error = $this->_query->errorInfo();
  61.             }
  62.         }
  63.         return $this;
  64.     }
  65.  
  66.     /**
  67.      *
  68.      * @return type
  69.      */
  70.     public function getErrors() {
  71.         return $this->_error;
  72.     }
  73.  
  74.     /**
  75.      *
  76.      * @return type
  77.      */
  78.     public function getResult() {
  79.         return $this->_result;
  80.     }
  81.  
  82.     /**
  83.      *
  84.      * @return type
  85.      */
  86.     public function getCount() {
  87.         return $this->_count;
  88.     }
  89.  
  90.     /**
  91.      *
  92.      * @return type
  93.      */
  94.     public function lastID() {
  95.         return $this->_pdo->lastInsertId();
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment