Advertisement
Guest User

Untitled

a guest
Dec 20th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. <?php
  2.  
  3. require_once '../initProjeto.php';
  4.  
  5. class Conexao{
  6.  
  7. private $host = DB_HOST;
  8. private $database = DB_NAME;
  9. private $gerenciador = DB_TYPE;
  10. private $user = DB_USER;
  11. private $password = DB_PASSWORD;
  12. public static $instance;
  13. public $conex = NULL;
  14.  
  15. public function __construct(){
  16. try {
  17. $strConexao = $this->gerenciador.':dbname='.$this->database.';host='.$this->host.';';
  18. $this->conex = new PDO($strConexao, $this->user, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", PDO::ATTR_PERSISTENT => true) );
  19. } catch (PDOException $e) {
  20.  
  21. die('Erro na conexão com o banco de dados: ' . $e->getMessage());
  22. }
  23. }
  24.  
  25. public function __destruct(){
  26. $this->conex = null;
  27. }
  28.  
  29. public static function getConnection(){
  30. if(!self::$instance) {
  31. self::$instance = new conexao();
  32. }
  33.  
  34. return self::$instance->conex;
  35. }
  36.  
  37. public function closeConex () {
  38. $this->conex = NULL;
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement