Advertisement
Guest User

PDO

a guest
Sep 9th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.69 KB | None | 0 0
  1. <?php
  2.  
  3. class database extends PDO {
  4.  
  5.     private $host=db_host;
  6.     private $dbname=db_name;
  7.     private $user=db_user;
  8.     private $pass=db_pass;
  9.     private $dbh; //database handler
  10.     private $error;
  11.     private $stmt; // statement
  12.  
  13.     public function __construct()
  14.     {
  15.             //Устанавливаем dsn
  16.         $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
  17.             //Определяем параметры
  18.         $options = array(
  19.  
  20.             PDO::ATTR_PERSISTENT => true, // постоянное подключение
  21.             PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  22.         );
  23.             //Создаем instance
  24.         try {
  25.             $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
  26.         }
  27.             // Обрабатываем ошибки
  28.         catch (PDOException $e) {
  29.             $this->error = $e->getMessage();
  30.         }
  31.     }
  32.     public function __destruct()
  33.     {
  34.         //Закрываем соединение с базой
  35.         $dbh=null;
  36.  
  37.     }
  38.  
  39.     public function query($query){
  40.         $this->stmt = $this->dbh->prepare($query);
  41.     }
  42.  
  43.  
  44.     public function bind($param, $value, $type = null){
  45.         //Определяем тип параметров
  46.         if (is_null($type)) {
  47.             switch (true) {
  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.             $this->stmt->bindValue($param, $value, $type);
  61.         }
  62.  
  63.     }
  64.  
  65.     public function execute(){
  66.         return $this->stmt->execute();
  67.     }
  68.     //Возвращает массив с результатами
  69.     public function resultset(){
  70.         $this->execute();
  71.         return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  72.     }
  73.     //Возвращает единичную строку с результатом
  74.     public function single(){
  75.         $this->execute();
  76.         return $this->stmt->fetch(PDO::FETCH_ASSOC);
  77.     }
  78.     //Возвращает количество найденных рядов
  79.     public function rowCount(){
  80.         return $this->stmt->rowCount();
  81.     }
  82.     //Возвращает id последнего insert
  83.     public function lastInsertId(){
  84.         return $this->dbh->lastInsertId();
  85.     }
  86.  
  87.     public function debugDumpParams(){
  88.         return $this->stmt->debugDumpParams();
  89.     }
  90.  
  91.  
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement