Guest User

mysql.php

a guest
Sep 26th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.33 KB | None | 0 0
  1. <?php
  2.  
  3. /* mal mit singleton tunen */
  4. class DB {
  5.    
  6.     private static $_db_username = "wevents";
  7.     private static $_db_password = "passwort123";
  8.     private static $_db_host = "127.0.0.1";
  9.     private static $_db_name = "events";
  10.     private static $_db;
  11.    
  12.     function __construct() {
  13.         try {
  14.         self::$_db = new PDO("mysql:host=" . self::$_db_host . ";dbname=" . self::$_db_name, self::$_db_username , self::$_db_password);
  15.     } catch(PDOException $e) {
  16.         echo"Datenbankverbindung gescheitert!";
  17.         die();
  18.         }
  19.     }
  20.    
  21.     function isUserLoggedIn(){
  22.         $stmt = self::$_db->prepare("SELECT userID FROM users WHERE session=:sid");
  23.         $sid = session_id();
  24.         $stmt->bindParam(":sid", $sid);
  25.         $stmt->execute();
  26.        
  27.         if($stmt->rowCount() === 1) {
  28.             return TRUE;
  29.             } else {
  30.             return FALSE;      
  31.         }
  32.     }
  33.    
  34.     function login($userMail, $pw){
  35.         $stmt = self::$_db->prepare("SELECT userID FROM users WHERE email=:usermail AND passwort=:pw");
  36.         $stmt->bindParam(":usermail", $userMail);
  37.         $stmt->bindParam(":pw", $pw);
  38.         $stmt->execute();
  39.        
  40.         if($stmt->rowCount() === 1) {
  41.             $stmt = self::$_db->prepare("UPDATE users SET session=:sid WHERE email=:usermail AND passwort=:pw");
  42.             $sid = session_id();
  43.             $stmt->bindParam(":sid", $sid);
  44.             $stmt->bindParam(":usermail", $userMail);
  45.             $stmt->bindParam(":pw", $pw);
  46.             $stmt->execute();
  47.             return TRUE;
  48.             } else {
  49.             return FALSE;      
  50.         }
  51.     }
  52.    
  53.     function logout(){
  54.         $stmt = self::$_db->prepare("UPDATE users SET session='' WHERE session=:sid");
  55.         $sid = session_id();
  56.         $stmt->bindParam(":sid", $sid);
  57.         $stmt->execute();
  58.         }
  59.    
  60.     function getAllEntries($sort = "DESC") {
  61.         if($sort != "ASC" && $sort != "DESC"){
  62.             return -1;
  63.             }
  64.            
  65.             $stmt = self::$_db->prepare("SELECT * FROM eintraege ORDER BY datum " . $sort);
  66.             $stmt->execute();
  67.             return $stmt->fetchAll(PDO::FETCH_ASSOC); /* ASSOC Konstante Assoziatives Array*/
  68.     }
  69.    
  70.     function createNews($autor, $datum, $headline, $text, $tags) {
  71.        
  72.         $stmt = self::$_db->prepare("INSERT INTO eintraege VALUES('', :autor, :datum, :headline, :text, :tags)");
  73.         $stmt->bindParam(":autor", $autor);
  74.         $stmt->bindParam(":datum", $datum);
  75.         $stmt->bindParam(":headline", $headline);
  76.         $stmt->bindParam(":text", $text);
  77.         $stmt->bindParam(":tags", $tags);
  78.         /*$stmt->execute();*/
  79.        
  80.         if ($stmt->execute()) {
  81.             return TRUE;
  82.             }
  83.         else {
  84.             return FALSE; }
  85.         }
  86.    
  87.     function createUser($email, $passwort, $name, $status) {
  88.        
  89.         $stmt = self::$_db->prepare("INSERT INTO users VALUES('', :email, :passwort, :name, :status, :session)");
  90.         $sid = session_id();
  91.         $stmt->bindParam(":email", $email);
  92.         $stmt->bindParam(":passwort", $passwort);
  93.         $stmt->bindParam(":name", $name);
  94.         $stmt->bindParam(":status", $status);
  95.         $stmt->bindParam(":session", $sid);
  96.         if ($stmt->execute()) {
  97.             return TRUE;
  98.             }
  99.         else {
  100.             return FALSE; }
  101.         }
  102.        
  103.     function getUserID() {
  104.         $stmt = self::$_db->prepare("SELECT userID FROM users WHERE session=:sid");
  105.         $sid = session_id();
  106.         $stmt->bindParam(":sid", $sid);    
  107.         $stmt->execute();
  108.        
  109.         return $stmt->fetch(PDO::FETCH_OBJ)->userID;
  110.    
  111.     }
  112.    
  113.     function getUserName() {
  114.        
  115.         $stmt = self::$_db->prepare("SELECT name FROM users WHERE session=:sid");
  116.         $sid = session_id();
  117.         $stmt->bindParam(":sid", $sid);    
  118.         $stmt->execute();
  119.        
  120.         $user = $stmt->fetch(PDO::FETCH_OBJ);
  121.        
  122.         return $user->name ;
  123.    
  124.     }
  125.    
  126. }
  127.    
  128. ?>
Advertisement
Add Comment
Please, Sign In to add comment