Advertisement
Guest User

Untitled

a guest
Jun 5th, 2023
54
0
25 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.35 KB | None | 0 0
  1. <?php
  2. /****************************************
  3.  * Core.php Datei
  4.  ***************************************/
  5. declare(strict_types=1);
  6.  
  7. session_start();
  8.  
  9. /**
  10.  * Hauptklasse mit Datenbank
  11.  */
  12. class Core {
  13.  
  14.     /**
  15.      * Handle zur verbundenen Datenbank
  16.      */
  17.     private static $link = null;
  18.  
  19.     /**
  20.      * Verbinde zur Datenbank und speichere das Handle $link
  21.      * @throws \Exception on error
  22.      */
  23.     public static function connectDB(): void {
  24.  
  25.         require('conf.php'); // Datenbank Zugangsdaten
  26.         Core::$link = new mysqli($conf["host"], $conf["user"], $conf["password"], $conf["name"]);
  27.  
  28.         // check connection
  29.         if (mysqli_connect_errno())
  30.             throw new \Exception('Connect failed: ' . mysqli_connect_error());
  31.  
  32.         if (!Core::$link->set_charset("utf8"))
  33.             throw new \Exception('Error loading character set utf8: ' . Core::$link->error);
  34.     }
  35.  
  36.     /**
  37.      * Schliesst die Datenbank (Braucht man aber fast nie, da die Datenbank \
  38.      * beim Beenden automatisch geschlossen wird.)
  39.      */
  40.     public static function closeDB() {
  41.         Core::$link->close();
  42.     }
  43.  
  44.     /**
  45.      * Get Staff List
  46.      * @param array $user Benutzer
  47.      * @return array Benutzerdaten
  48.      * @throws \Exception on error
  49.      */
  50.     public static function getStaffList(array $user): array {
  51.         try {
  52.             Core::connectDB();
  53.  
  54.             if ($user['role'] === 'admin') {
  55.                 $stmt = Core::$link->prepare('SELECT * FROM staff');
  56.             }
  57.             else {
  58.                 $stmt = Core::$link->prepare('SELECT * FROM staff WHERE surname=?');
  59.                 $stmt->bind_param('s', $user['name']);
  60.             }
  61.  
  62.             $stmt->execute();
  63.  
  64.             $result = $stmt->get_result();
  65.  
  66.             if (!$result)
  67.                 throw new \Exception(mysqli_error(Core::$link));
  68.  
  69.             $staff = [];
  70.  
  71.             while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  72.                 if ($row['birthday'] !== '0000-00-00')
  73.                     $row['birthday'] = date("d.m.Y", strtotime($row['birthday']));
  74.                 else
  75.                     $row['birthday'] = '';
  76.  
  77.                 $staff[] = $row;
  78.             }
  79.  
  80.             return $staff;
  81.         }
  82.         catch(Exception $e) {
  83.             throw new \Exception($e->getMessage());
  84.         }
  85.     }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement