bssanchez93

Clase Abstracta PHP - PDO

Nov 20th, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.84 KB | None | 0 0
  1. <?php
  2.  
  3. abstract class ClsABSDataBase {
  4.  
  5.     private static $host = "localhost";
  6.     private static $user = "root";
  7.     private static $pass = "";
  8.     protected $db = "nombre_basededatos";
  9.     private $conex;
  10.     protected $query;
  11.     protected $data = array();
  12.     protected $result = array();
  13.  
  14.     abstract protected function get($query, $data);
  15.  
  16.     abstract protected function set($query, $data);
  17.  
  18.     abstract protected function edit($query, $data);
  19.  
  20.     private function conDB($db = '') {
  21.         try {
  22.             $this->db = ($db != '') ? $db : $this->db;
  23.             $this->conex = new PDO("mysql:host=" . self::$host . ";dbname=$this->db", self::$user, self::$pass);
  24.             $this->conex->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  25.         } catch (PDOException $ex) {
  26.             die("Error SQL [" . $ex->getCode() . "] " . $ex->getMessage());
  27.         }
  28.     }
  29.  
  30.     private function desconDB() {
  31.         try {
  32.             $this->conex = null;
  33.         } catch (PDOException $ex) {
  34.             die("Error SQL [" . $ex->getCode() . "] " . $ex->getMessage());
  35.         }
  36.     }
  37.  
  38.     protected function getResult() {
  39.         try {
  40.             $this->conDB();
  41.             $eSql = $this->conex->prepare($this->query);
  42.             $eSql->execute($this->data);
  43.  
  44.             $this->result = $eSql->fetchAll(PDO::FETCH_ASSOC);
  45.             $this->desconDB();
  46.         } catch (PDOException $ex) {
  47.             die("Error SQL [" . $ex->getCode() . "] " . $ex->getMessage());
  48.         }
  49.     }
  50.  
  51.     protected function query() {
  52.         try {
  53.             $this->conDB();
  54.             $eSql = $this->conex->prepare($this->query);
  55.             $eSql->execute();
  56.             $this->desconDB();
  57.         } catch (PDOException $ex) {
  58.             die(md_err($ex->getCode(), $ex->getMessage(), "Error SQL"));
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment