Advertisement
Guest User

Conexion.inc.php

a guest
Aug 17th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.80 KB | None | 0 0
  1. <?php
  2.  
  3. class Conexion {
  4.  
  5.     private static $Conexion;
  6.  
  7.     public static function abrir_conexion(){
  8.         if (!isset(self::$Conexion)){
  9.             try {
  10.                 include_once 'config.inc.php';
  11.  
  12.                 self::$conexion = new PDO("mysql:host=$nombre_servidor; dbname=$nombre_base_datos", $nombre_usuario, $password);
  13.                 self::$Conexion -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  14.                 self::$Conexion -> exec ("SET CHARACTER SET utf8");
  15.  
  16.                 print "Conexion abierta";
  17.  
  18.             } catch (PDOException $ex){
  19.                 print "ERROR: " . $ex -> getMessage() . "<br>";
  20.                 die();
  21.  
  22.             }
  23.         }
  24.     }
  25.  
  26.     public static function cerrar_conexion(){
  27.         if (isset (self::$Conexion)) {
  28.             self::$Conexion = null;
  29.             print "cerrar conexion";       
  30.         }
  31.     }
  32.  
  33.     public static function obtener_conexion(){
  34.         return self::$Conexion;
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement