Advertisement
Guest User

Untitled

a guest
Feb 17th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | None | 0 0
  1. class Database {
  2.     public $isConn;
  3.     protected $datab;
  4.    
  5.     // Connect zur DB
  6.     public function __construct($username = "root_portal", $password = "Mxt59l$4", $host = "localhost", $dbname = "db12354864"){
  7.         $this->isConn = TRUE;
  8.         try{
  9.             $this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password);  
  10.                                                                                        
  11.             $this->datab->setAttribute(PDO::ATRR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  12.             $this->datab->setAttribute(PDO::ATRR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  13.            
  14.         }catch(PDOException $e){
  15.             throw new Exception($e->getMessage());
  16.            
  17.         }
  18.     }  
  19.     // Verbindung beenden
  20.     public function Disconnect(){
  21.         $this->datab = NULL;
  22.         $this->isConn = FALSE;
  23.     }
  24.     // Hole eine Spalte
  25.     public function getRow($query, $params = array()){
  26.         try {
  27.             $stmt = $this->datab->prepare($query);
  28.             $stmt->execute($params);
  29.             return $stmt->fetch();
  30.         }catch (PDOException $e){
  31.             throw new Exception($e->getMessage());
  32.         }
  33.     }  
  34. }
  35.  
  36. test php
  37. <?php
  38.  
  39. require_once 'module/mysqli.php';
  40.  
  41. $db = new Database();
  42.  
  43. $getRow = $db->getRow("SELECT * FROM user WHERE id = ?", $params = array(1));
  44. var_dump($getRow);
  45.  
  46. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement