From 97a16b855410ba1af6eb6bab96514b3a5d86610f Mon Sep 17 00:00:00 2001 From: Niek Rabelink Date: Tue, 8 Sep 2026 13:26:53 +0200 Subject: [PATCH] feat: enhance database connection error handling and add MySQL driver check --- .env.example | 1 + config/database.php | 49 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index a346f22..a7eaf40 100644 --- a/.env.example +++ b/.env.example @@ -13,3 +13,4 @@ APP_URL="https://uw-domein.nl" # Applicatie Geheime Sleutel (willekeurige tekenreeks) APP_SECRET="fociz-maria-db-super-secret-key-998877" + diff --git a/config/database.php b/config/database.php index e8cbffc..305cc45 100644 --- a/config/database.php +++ b/config/database.php @@ -9,6 +9,36 @@ function get_db_connection(): PDO { static $pdo = null; if ($pdo === null) { + // Controleer of de PDO MySQL/MariaDB driver überhaupt aanwezig is in deze PHP installatie + $availableDrivers = PDO::getAvailableDrivers(); + if (!in_array('mysql', $availableDrivers)) { + $driversList = !empty($availableDrivers) ? implode(', ', $availableDrivers) : 'geen'; + die(" +
+
+

+ ⚠️ PHP MySQL Driver Ontbreekt (pdo_mysql) +

+

+ De PHP-installatie op uw server heeft momenteel de pdo_mysql extensie niet ingeschakeld. Zonder deze driver kan PHP geen verbinding maken met MariaDB. +

+
+ Oplossing per servertype: +
    +
  • Ubuntu / Debian:
    sudo apt install php-mysql && sudo systemctl restart apache2 (of php-fpm)
  • +
  • CentOS / RHEL / AlmaLinux:
    sudo dnf install php-mysqlnd && sudo systemctl restart httpd
  • +
  • cPanel / DirectAdmin / Plesk:
    Ga naar Select PHP Version → Extensions en vink pdo_mysql (en nd_pdo_mysql of mysqlnd) aan.
  • +
  • Windows / XAMPP:
    Verwijder de puntkomma ; voor extension=pdo_mysql in uw php.ini en herstart Apache.
  • +
+
+
+ PHP Versie: " . PHP_VERSION . " • Momenteel actieve drivers: " . htmlspecialchars($driversList) . " +
+
+
+ "); + } + $dsn = sprintf( 'mysql:host=%s;port=%s;dbname=%s;charset=%s', DB_HOST, @@ -21,6 +51,7 @@ function get_db_connection(): PDO { PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, + PDO::ATTR_TIMEOUT => 5, ]; try { @@ -38,10 +69,21 @@ function get_db_connection(): PDO { ensure_tables_exist($pdo); return $pdo; } catch (PDOException $ex) { - die("Database verbindingsfout: Kan database '" . htmlspecialchars(DB_NAME) . "' niet aanmaken of vinden. Maak deze handmatig aan in MariaDB."); + die("Database verbindingsfout: Kan database '" . htmlspecialchars(DB_NAME) . "' niet aanmaken of vinden op " . htmlspecialchars(DB_HOST) . ". Maak deze handmatig aan in MariaDB."); } } - die("MariaDB Verbindingsfout: " . htmlspecialchars($e->getMessage()) . "
Controleer uw instellingen in config/config.php of .env"); + die(" +
+

MariaDB Verbindingsfout (" . htmlspecialchars((string)$e->getCode()) . ")

+

" . htmlspecialchars($e->getMessage()) . "

+
+ Host: " . htmlspecialchars(DB_HOST) . "
+ Poort: " . htmlspecialchars(DB_PORT) . "
+ Database: " . htmlspecialchars(DB_NAME) . "
+ Gebruiker: " . htmlspecialchars(DB_USER) . " +
+
+ "); } } @@ -62,7 +104,6 @@ function ensure_tables_exist(PDO $pdo): void { } } } catch (PDOException $e) { - // Tabellen bestaan waarschijnlijk al of er is een permissiebeperking + // Tabellen bestaan waarschijnlijk al } } -