Advertisement
Guest User

Untitled

a guest
Jun 5th, 2023
61
0
26 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.78 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.      * Konfgurationsdatei
  16.      * ACHTUNG: Diese Datei muss geschützt werden, damit sie nicht jeder aufrufen kann!
  17.      * HINWEIS: Schlauer wäre es eine conf.php zu machen ;)
  18.      */
  19.     private static $config_file = "conf.ini";
  20.  
  21.     /**
  22.      * Handle zur verbundenen Datenbank
  23.      */
  24.     private static $link = null;
  25.  
  26.     /**
  27.      * Hole Konfigurationsdaten aus Datei
  28.      * @return array Datenbank Zugangsdaten
  29.      * @throws \Exception on error
  30.      */
  31.     private static function getDBConfig(): array {
  32.         if ($conf = parse_ini_file(Core::$config_file))
  33.             return $conf;
  34.         else
  35.             throw new \Exception('Fehler in der Konfgurationsdatei.');
  36.     }
  37.  
  38.     /**
  39.      * Verbinde zur Datenbank und speichere das Handle $link
  40.      * @throws \Exception on error
  41.      */
  42.     public static function connectDB(): void {
  43.         $conf = Core::getDBConfig();
  44.  
  45.         Core::$link = new mysqli($conf["host"], $conf["user"], $conf["password"], $conf["name"]);
  46.  
  47.         // check connection
  48.         if (mysqli_connect_errno())
  49.             throw new \Exception('Connect failed: ' . mysqli_connect_error());
  50.  
  51.         if (!Core::$link->set_charset("utf8"))
  52.             throw new \Exception('Error loading character set utf8: ' . Core::$link->error);
  53.     }
  54.  
  55.     /**
  56.      * Schliesst die Datenbank (Braucht man aber fast nie, da die Datenbank \
  57.      * beim Beenden automatisch geschlossen wird.)
  58.      */
  59.     public static function closeDB() {
  60.         Core::$link->close();
  61.     }
  62.  
  63.     /**
  64.      * Get Staff List
  65.      * @param string $userName Benutzername
  66.      * @return array Benutzerdaten
  67.      * @throws \Exception on error
  68.      */
  69.     public static function getStaffList(string $userName): array {
  70.  
  71.         try {
  72.             Core::connectDB();
  73.  
  74.             $stmt = Core::$link->prepare('SELECT * FROM staff WHERE surname=?');
  75.             $stmt->bind_param('s', $userName);
  76.             $stmt->execute();
  77.  
  78.             $result = $stmt->get_result();
  79.  
  80.             if (!$result)
  81.                 throw new \Exception(mysqli_error(Core::$link));
  82.  
  83.             $staff = [];
  84.  
  85.             while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  86.                 if ($row['birthday'] !== '0000-00-00')
  87.                     $row['birthday'] = date("d.m.Y", strtotime($row['birthday']));
  88.                 else
  89.                     $row['birthday'] = '';
  90.  
  91.                 $staff[] = $row;
  92.             }
  93.  
  94.             return $staff;
  95.         }
  96.         catch(Exception $e) {
  97.             throw new \Exception($e->getMessage());
  98.         }
  99.     }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement