Advertisement
Guest User

/config/includes/bdd/database.php

a guest
Nov 25th, 2019
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.13 KB | None | 0 0
  1. <?php
  2.  
  3. class Database {
  4.    
  5.     private $tokenAuth;
  6.     private $pdo;
  7.    
  8.     private function _construct() {
  9.         $this->tokenAuth = array(
  10.             'bdname' => 'bd_name_xxx',
  11.             'host' => 'localhost'
  12.         ); 
  13.     }
  14.    
  15.     /**
  16.     * Connexion à la base de données.
  17.     */
  18.     private function getPDO() {
  19.         try {
  20.             if($this->pdo == null){
  21.                 $user = 'user_xxx';
  22.                 $password = 'xxxx';
  23.                
  24.                 $pdo = new PDO('mysql:host=' . $this->tokenAuth['host'] . ';dbname=' . $this->tokenAuth['dbname'] . ';charset=UTF8', $user, $password);
  25.                 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  26.                 $this->pdo = $pdo;
  27.             }
  28.         } catch (PDOException $e) {
  29.             echo 'Connexion bdd = false <br/>Raison = ';
  30.             die('Erreur : ' .$e->getMessage());
  31.         }
  32.     }
  33.    
  34.     /**
  35.     * Appel de la BDD
  36.     */
  37.     public function BDD() {
  38.         $this->getPDO();
  39.     }
  40.    
  41.     /**
  42.     * Fermer la connexion à la base de données.
  43.     */
  44.     public function shutdownbdd() {
  45.         $this->pdo = null;
  46.         return true;
  47.     }
  48.    
  49.     /**
  50.     * Requête query à la base de données.
  51.     */
  52.     public function query($statement) {
  53.         $request = $this->getPDO()->query($statement);
  54.         $this->shutdown();
  55.         return $result;
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement