Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /****************************************
- * Core.php Datei
- ***************************************/
- declare(strict_types=1);
- session_start();
- /**
- * Hauptklasse mit Datenbank
- */
- class Core {
- /**
- * Handle zur verbundenen Datenbank
- */
- private static $link = null;
- /**
- * Verbinde zur Datenbank und speichere das Handle $link
- * @throws \Exception on error
- */
- public static function connectDB(): void {
- require('conf.php'); // Datenbank Zugangsdaten
- Core::$link = new mysqli($conf["host"], $conf["user"], $conf["password"], $conf["name"]);
- // check connection
- if (mysqli_connect_errno())
- throw new \Exception('Connect failed: ' . mysqli_connect_error());
- if (!Core::$link->set_charset("utf8"))
- throw new \Exception('Error loading character set utf8: ' . Core::$link->error);
- }
- /**
- * Schliesst die Datenbank (Braucht man aber fast nie, da die Datenbank \
- * beim Beenden automatisch geschlossen wird.)
- */
- public static function closeDB() {
- Core::$link->close();
- }
- /**
- * Get Staff List
- * @param array $user Benutzer
- * @return array Benutzerdaten
- * @throws \Exception on error
- */
- public static function getStaffList(array $user): array {
- try {
- Core::connectDB();
- if ($user['role'] === 'admin') {
- $stmt = Core::$link->prepare('SELECT * FROM staff');
- }
- else {
- $stmt = Core::$link->prepare('SELECT * FROM staff WHERE surname=?');
- $stmt->bind_param('s', $user['name']);
- }
- $stmt->execute();
- $result = $stmt->get_result();
- if (!$result)
- throw new \Exception(mysqli_error(Core::$link));
- $staff = [];
- while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
- if ($row['birthday'] !== '0000-00-00')
- $row['birthday'] = date("d.m.Y", strtotime($row['birthday']));
- else
- $row['birthday'] = '';
- $staff[] = $row;
- }
- return $staff;
- }
- catch(Exception $e) {
- throw new \Exception($e->getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement