Advertisement
Guest User

Untitled

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