Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.01 KB | None | 0 0
  1. <?php
  2.     class db {
  3.         /**
  4.          * @var PDO
  5.          */
  6.         private $conn;
  7.         private $host;
  8.         private $user;
  9.         private $password;
  10.         private $baseName;
  11.         private $port;
  12.         private $Debug;
  13.  
  14.         function __construct($params=array()) {
  15.             $this->conn = false;
  16.             $this->host = 'localhost'; //hostname
  17.             $this->user = 'root'; //username
  18.             $this->password = 'root'; //password
  19.             $this->baseName = 'kbsjaar1'; //name of your database
  20.             $this->port = '8888';
  21.             $this->debug = true;
  22.             $this->connect();
  23.         }
  24.  
  25.         function __destruct() {
  26.             $this->disconnect();
  27.         }
  28.  
  29.         function connect() {
  30.             if (!$this->conn) {
  31.                 try {
  32.                     $this->conn = new PDO('mysql:host='.$this->host.';dbname='.$this->baseName.'', $this->user, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
  33.                 }
  34.                 catch (Exception $e) {
  35.                     die('Erreur : ' . $e->getMessage());
  36.                 }
  37.  
  38.                 if (!$this->conn) {
  39.                     $this->status_fatal = true;
  40.                     echo 'Connection BDD failed';
  41.                     die();
  42.                 }
  43.                 else {
  44.                     $this->status_fatal = false;
  45.                 }
  46.             }
  47.  
  48.             return $this->conn;
  49.         }
  50.  
  51.         function disconnect() {
  52.             if ($this->conn) {
  53.                 $this->conn = null;
  54.             }
  55.         }
  56.  
  57.         function getOne($query) {
  58.             $result = $this->conn->prepare($query);
  59.             $ret = $result->execute();
  60.             if (!$ret) {
  61.                 echo 'PDO::errorInfo():';
  62.                 echo '<br />';
  63.                 echo 'error SQL: '.$query;
  64.                 die();
  65.             }
  66.             $result->setFetchMode(PDO::FETCH_ASSOC);
  67.             $reponse = $result->fetch();
  68.  
  69.             return $reponse;
  70.         }
  71.  
  72.         function getAll($query) {
  73.             $result = $this->conn->prepare($query);
  74.             $ret = $result->execute();
  75.             if (!$ret) {
  76.                 echo 'PDO::errorInfo():';
  77.                 echo '<br />';
  78.                 echo 'error SQL: '.$query;
  79.                 die();
  80.             }
  81.             $result->setFetchMode(PDO::FETCH_ASSOC);
  82.             $reponse = $result->fetchAll();
  83.  
  84.             return $reponse;
  85.         }
  86.  
  87.         function execute($query) {
  88.             if (!$response = $this->conn->exec($query)) {
  89.                 echo 'PDO::errorInfo():';
  90.                 echo '<br />';
  91.                 echo 'error SQL: '.$query;
  92.                 die();
  93.             }
  94.             return $response;
  95.         }
  96.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement