Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* mal mit singleton tunen */
- class DB {
- private static $_db_username = "wevents";
- private static $_db_password = "passwort123";
- private static $_db_host = "127.0.0.1";
- private static $_db_name = "events";
- private static $_db;
- function __construct() {
- try {
- self::$_db = new PDO("mysql:host=" . self::$_db_host . ";dbname=" . self::$_db_name, self::$_db_username , self::$_db_password);
- } catch(PDOException $e) {
- echo"Datenbankverbindung gescheitert!";
- die();
- }
- }
- function isUserLoggedIn(){
- $stmt = self::$_db->prepare("SELECT userID FROM users WHERE session=:sid");
- $sid = session_id();
- $stmt->bindParam(":sid", $sid);
- $stmt->execute();
- if($stmt->rowCount() === 1) {
- return TRUE;
- } else {
- return FALSE;
- }
- }
- function login($userMail, $pw){
- $stmt = self::$_db->prepare("SELECT userID FROM users WHERE email=:usermail AND passwort=:pw");
- $stmt->bindParam(":usermail", $userMail);
- $stmt->bindParam(":pw", $pw);
- $stmt->execute();
- if($stmt->rowCount() === 1) {
- $stmt = self::$_db->prepare("UPDATE users SET session=:sid WHERE email=:usermail AND passwort=:pw");
- $sid = session_id();
- $stmt->bindParam(":sid", $sid);
- $stmt->bindParam(":usermail", $userMail);
- $stmt->bindParam(":pw", $pw);
- $stmt->execute();
- return TRUE;
- } else {
- return FALSE;
- }
- }
- function logout(){
- $stmt = self::$_db->prepare("UPDATE users SET session='' WHERE session=:sid");
- $sid = session_id();
- $stmt->bindParam(":sid", $sid);
- $stmt->execute();
- }
- function getAllEntries($sort = "DESC") {
- if($sort != "ASC" && $sort != "DESC"){
- return -1;
- }
- $stmt = self::$_db->prepare("SELECT * FROM eintraege ORDER BY datum " . $sort);
- $stmt->execute();
- return $stmt->fetchAll(PDO::FETCH_ASSOC); /* ASSOC Konstante Assoziatives Array*/
- }
- function createNews($autor, $datum, $headline, $text, $tags) {
- $stmt = self::$_db->prepare("INSERT INTO eintraege VALUES('', :autor, :datum, :headline, :text, :tags)");
- $stmt->bindParam(":autor", $autor);
- $stmt->bindParam(":datum", $datum);
- $stmt->bindParam(":headline", $headline);
- $stmt->bindParam(":text", $text);
- $stmt->bindParam(":tags", $tags);
- /*$stmt->execute();*/
- if ($stmt->execute()) {
- return TRUE;
- }
- else {
- return FALSE; }
- }
- function createUser($email, $passwort, $name, $status) {
- $stmt = self::$_db->prepare("INSERT INTO users VALUES('', :email, :passwort, :name, :status, :session)");
- $sid = session_id();
- $stmt->bindParam(":email", $email);
- $stmt->bindParam(":passwort", $passwort);
- $stmt->bindParam(":name", $name);
- $stmt->bindParam(":status", $status);
- $stmt->bindParam(":session", $sid);
- if ($stmt->execute()) {
- return TRUE;
- }
- else {
- return FALSE; }
- }
- function getUserID() {
- $stmt = self::$_db->prepare("SELECT userID FROM users WHERE session=:sid");
- $sid = session_id();
- $stmt->bindParam(":sid", $sid);
- $stmt->execute();
- return $stmt->fetch(PDO::FETCH_OBJ)->userID;
- }
- function getUserName() {
- $stmt = self::$_db->prepare("SELECT name FROM users WHERE session=:sid");
- $sid = session_id();
- $stmt->bindParam(":sid", $sid);
- $stmt->execute();
- $user = $stmt->fetch(PDO::FETCH_OBJ);
- return $user->name ;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment