Advertisement
Guest User

Untitled

a guest
Jan 11th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.29 KB | None | 0 0
  1. <?php
  2. class DatabaseTest
  3. {
  4.     const DSN = DSN_DB;
  5.     const USER = USER_DB;
  6.     const PASSWORD = PASSWORD_DB;
  7.     const OPTIONS = OPTIONS_PDO;
  8.     protected $connection_status;
  9.     protected $error_message;
  10.     protected $db_connect;
  11.     protected function connect()
  12.     {
  13.         try {
  14.             $this->db_connect = new PDO(self::DSN, self::USER, self::PASSWORD, self::OPTIONS );
  15.             //ejecutamos la conexion
  16.             $this->connection_status = true; //asignamos true al atributo
  17.             $this->error_message = "";
  18.         } catch (PDOException $e) { //entramos si se encuentra un error o exeption
  19.             $this->connection_status = false; //asignamos el valor a false
  20.             $this->error_message = "error en: ".$e->getMessage(); // asignamos el mensaje del error al atributo
  21.         }
  22.     }
  23.  
  24.     protected function getConnectionStatus()
  25.     {//metodo que retorna el estatus de la conexion, lo implementamos en cada metodo que opera con la conexion con la base de datos (INSERT, SELECT, UPDATE, DELETE)
  26.         return $this->connection_status;
  27.     }
  28.  
  29.     protected function getErrorMessage()
  30.     { //metodo que nos devuelve el mensaje de error si no llega a darse la conexion
  31.         return $this->error_message;
  32.     }
  33.  
  34.     protected function disconnect()
  35.     {//metodo implementado para simular el cierre de conexion
  36.         $this->connection_status = false;
  37.         $this->db_connect = null;
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement