fix: explicitly check if database and tables already exist before creating
This commit is contained in:
+109
-85
@@ -8,111 +8,135 @@ require_once __DIR__ . '/config.php';
|
|||||||
function get_db_connection(): PDO {
|
function get_db_connection(): PDO {
|
||||||
static $pdo = null;
|
static $pdo = null;
|
||||||
|
|
||||||
if ($pdo === null) {
|
if ($pdo !== null) {
|
||||||
// Controleer of de PDO MySQL/MariaDB driver überhaupt aanwezig is in deze PHP installatie
|
return $pdo;
|
||||||
$availableDrivers = PDO::getAvailableDrivers();
|
}
|
||||||
if (!in_array('mysql', $availableDrivers)) {
|
|
||||||
$driversList = !empty($availableDrivers) ? implode(', ', $availableDrivers) : 'geen';
|
// 1. Controleer of de PDO MySQL/MariaDB driver aanwezig is in PHP
|
||||||
die("
|
$availableDrivers = PDO::getAvailableDrivers();
|
||||||
<div style='font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, sans-serif; background: #090d16; color: #f1f5f9; min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px;'>
|
if (!in_array('mysql', $availableDrivers)) {
|
||||||
<div style='max-width: 580px; background: #111827; border: 1px solid #ef4444; border-radius: 16px; padding: 30px; box-shadow: 0 20px 25px -5px rgba(0,0,0,0.5);'>
|
$driversList = !empty($availableDrivers) ? implode(', ', $availableDrivers) : 'geen';
|
||||||
<h2 style='color: #ef4444; margin-top: 0; display: flex; align-items: center; gap: 8px;'>
|
die("
|
||||||
⚠️ PHP MySQL Driver Ontbreekt (pdo_mysql)
|
<div style='font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, sans-serif; background: #090d16; color: #f1f5f9; min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px;'>
|
||||||
</h2>
|
<div style='max-width: 580px; background: #111827; border: 1px solid #ef4444; border-radius: 16px; padding: 30px; box-shadow: 0 20px 25px -5px rgba(0,0,0,0.5);'>
|
||||||
<p style='color: #94a3b8; font-size: 14px; line-height: 1.6;'>
|
<h2 style='color: #ef4444; margin-top: 0;'>⚠️ PHP MySQL Driver Ontbreekt (pdo_mysql)</h2>
|
||||||
De PHP-installatie op uw server heeft momenteel de <strong>pdo_mysql</strong> extensie niet ingeschakeld. Zonder deze driver kan PHP geen verbinding maken met MariaDB.
|
<p style='color: #94a3b8; font-size: 14px;'>De extensie <strong>pdo_mysql</strong> is niet actief in deze PHP-omgeving.</p>
|
||||||
</p>
|
</div>
|
||||||
<div style='background: #0b0f17; border: 1px solid #1f2937; border-radius: 10px; padding: 15px; margin: 20px 0;'>
|
</div>
|
||||||
<strong style='color: #38bdf8; font-size: 13px; display: block; margin-bottom: 8px;'>Oplossing per servertype:</strong>
|
");
|
||||||
<ul style='color: #e2e8f0; font-size: 13px; margin: 0; padding-left: 20px; line-height: 1.8;'>
|
}
|
||||||
<li><strong>Ubuntu / Debian:</strong><br><code style='color: #4ade80; background: #000; padding: 2px 6px; border-radius: 4px;'>sudo apt install php-mysql && sudo systemctl restart apache2</code> (of php-fpm)</li>
|
|
||||||
<li><strong>CentOS / RHEL / AlmaLinux:</strong><br><code style='color: #4ade80; background: #000; padding: 2px 6px; border-radius: 4px;'>sudo dnf install php-mysqlnd && sudo systemctl restart httpd</code></li>
|
$options = [
|
||||||
<li><strong>cPanel / DirectAdmin / Plesk:</strong><br>Ga naar <em>Select PHP Version → Extensions</em> en vink <strong>pdo_mysql</strong> (en <strong>nd_pdo_mysql</strong> of <strong>mysqlnd</strong>) aan.</li>
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||||
<li><strong>Windows / XAMPP:</strong><br>Verwijder de puntkomma <code>;</code> voor <code>extension=pdo_mysql</code> in uw <code>php.ini</code> en herstart Apache.</li>
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||||
</ul>
|
PDO::ATTR_EMULATE_PREPARES => false,
|
||||||
</div>
|
PDO::ATTR_TIMEOUT => 5,
|
||||||
<div style='font-size: 11px; color: #64748b;'>
|
];
|
||||||
PHP Versie: " . PHP_VERSION . " • Momenteel actieve drivers: " . htmlspecialchars($driversList) . "
|
if (defined('PDO::MYSQL_ATTR_MULTI_STATEMENTS')) {
|
||||||
|
$options[PDO::MYSQL_ATTR_MULTI_STATEMENTS] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$dsnWithDb = sprintf(
|
||||||
|
'mysql:host=%s;port=%s;dbname=%s;charset=%s',
|
||||||
|
DB_HOST,
|
||||||
|
DB_PORT,
|
||||||
|
DB_NAME,
|
||||||
|
DB_CHARSET
|
||||||
|
);
|
||||||
|
|
||||||
|
// 2. CHECK: Bestaat de database al?
|
||||||
|
// We proberen eerst direct verbinding te maken met de database.
|
||||||
|
// Als de database al bestaat, lukt dit direct zonder database-aanmaakrechten te vereisen!
|
||||||
|
try {
|
||||||
|
$pdo = new PDO($dsnWithDb, DB_USER, DB_PASS, $options);
|
||||||
|
|
||||||
|
// De database bestaat al! Controleer enkel of de tabellen al aanwezig zijn
|
||||||
|
ensure_tables_exist($pdo);
|
||||||
|
return $pdo;
|
||||||
|
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
// Foutcode 1049 betekent: Database 'DB_NAME' bestaat nog NIET op de MariaDB server
|
||||||
|
if ($e->getCode() == 1049 || str_contains($e->getMessage(), 'Unknown database')) {
|
||||||
|
// Probeer de database eenmalig aan te maken indien de gebruiker CREATE rechten heeft
|
||||||
|
try {
|
||||||
|
$serverDsn = sprintf('mysql:host=%s;port=%s;charset=%s', DB_HOST, DB_PORT, DB_CHARSET);
|
||||||
|
$serverPdo = new PDO($serverDsn, DB_USER, DB_PASS, $options);
|
||||||
|
$serverPdo->exec(sprintf(
|
||||||
|
'CREATE DATABASE IF NOT EXISTS `%s` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci',
|
||||||
|
DB_NAME
|
||||||
|
));
|
||||||
|
|
||||||
|
// Nu opnieuw verbinden met de nieuw aangemaakte database
|
||||||
|
$pdo = new PDO($dsnWithDb, DB_USER, DB_PASS, $options);
|
||||||
|
ensure_tables_exist($pdo);
|
||||||
|
return $pdo;
|
||||||
|
|
||||||
|
} catch (PDOException $createEx) {
|
||||||
|
die("
|
||||||
|
<div style='font-family: sans-serif; background: #090d16; color: #fff; padding: 40px; text-align: center; min-height: 100vh; display: flex; align-items: center; justify-content: center;'>
|
||||||
|
<div style='max-width: 500px; background: #111827; border: 1px solid #f59e0b; border-radius: 16px; padding: 30px; text-align: left;'>
|
||||||
|
<h2 style='color: #f59e0b; margin-top: 0;'>⚠️ Database '" . htmlspecialchars(DB_NAME) . "' bestaat nog niet</h2>
|
||||||
|
<p style='color: #94a3b8; font-size: 14px; line-height: 1.6;'>
|
||||||
|
De database <strong>" . htmlspecialchars(DB_NAME) . "</strong> bestaat nog niet op MariaDB server <strong>" . htmlspecialchars(DB_HOST) . "</strong> en de gebruiker <em>" . htmlspecialchars(DB_USER) . "</em> heeft geen rechten om automatisch databases aan te maken.
|
||||||
|
</p>
|
||||||
|
<p style='color: #cbd5e1; font-size: 13px;'>
|
||||||
|
Maak de database <code>" . htmlspecialchars(DB_NAME) . "</code> eenmalig aan via phpMyAdmin, HeidiSQL of de MariaDB CLI:
|
||||||
|
</p>
|
||||||
|
<code style='display: block; background: #000; color: #4ade80; padding: 10px; border-radius: 8px; font-size: 12px; margin: 10px 0;'>
|
||||||
|
CREATE DATABASE IF NOT EXISTS `" . htmlspecialchars(DB_NAME) . "` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||||
|
</code>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
");
|
||||||
");
|
|
||||||
}
|
|
||||||
|
|
||||||
$dsn = sprintf(
|
|
||||||
'mysql:host=%s;port=%s;dbname=%s;charset=%s',
|
|
||||||
DB_HOST,
|
|
||||||
DB_PORT,
|
|
||||||
DB_NAME,
|
|
||||||
DB_CHARSET
|
|
||||||
);
|
|
||||||
|
|
||||||
$options = [
|
|
||||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
||||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
||||||
PDO::ATTR_EMULATE_PREPARES => false,
|
|
||||||
PDO::ATTR_TIMEOUT => 5,
|
|
||||||
];
|
|
||||||
if (defined('PDO::MYSQL_ATTR_MULTI_STATEMENTS')) {
|
|
||||||
$options[PDO::MYSQL_ATTR_MULTI_STATEMENTS] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
$pdo = new PDO($dsn, DB_USER, DB_PASS, $options);
|
|
||||||
// Zorg ervoor dat tabellen bestaan
|
|
||||||
ensure_tables_exist($pdo);
|
|
||||||
} catch (PDOException $e) {
|
|
||||||
// Als de database zelf nog niet bestaat, probeer deze aan te maken indien gebruiker rechten heeft
|
|
||||||
if ($e->getCode() == 1049) {
|
|
||||||
try {
|
|
||||||
$tempDsn = sprintf('mysql:host=%s;port=%s;charset=%s', DB_HOST, DB_PORT, DB_CHARSET);
|
|
||||||
$tempPdo = new PDO($tempDsn, DB_USER, DB_PASS, $options);
|
|
||||||
$tempPdo->exec(sprintf('CREATE DATABASE IF NOT EXISTS `%s` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci', DB_NAME));
|
|
||||||
$pdo = new PDO($dsn, DB_USER, DB_PASS, $options);
|
|
||||||
ensure_tables_exist($pdo);
|
|
||||||
return $pdo;
|
|
||||||
} catch (PDOException $ex) {
|
|
||||||
die("Database verbindingsfout: Kan database '" . htmlspecialchars(DB_NAME) . "' niet aanmaken of vinden op " . htmlspecialchars(DB_HOST) . ". Maak deze handmatig aan in MariaDB.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
die("
|
}
|
||||||
<div style='font-family: sans-serif; background: #090d16; color: #fff; padding: 40px; text-align: center;'>
|
|
||||||
<h2 style='color: #ef4444;'>MariaDB Verbindingsfout (" . htmlspecialchars((string)$e->getCode()) . ")</h2>
|
// Andere verbindingsfout (bijv. verkeerd wachtwoord, poort of host onbereikbaar)
|
||||||
<p style='color: #cbd5e1;'>" . htmlspecialchars($e->getMessage()) . "</p>
|
die("
|
||||||
<div style='background: #111827; display: inline-block; padding: 15px; border-radius: 8px; text-align: left; font-size: 13px; margin-top: 15px;'>
|
<div style='font-family: sans-serif; background: #090d16; color: #fff; padding: 40px; text-align: center; min-height: 100vh; display: flex; align-items: center; justify-content: center;'>
|
||||||
|
<div style='max-width: 520px; background: #111827; border: 1px solid #ef4444; border-radius: 16px; padding: 30px; text-align: left;'>
|
||||||
|
<h2 style='color: #ef4444; margin-top: 0;'>MariaDB Verbindingsfout (" . htmlspecialchars((string)$e->getCode()) . ")</h2>
|
||||||
|
<p style='color: #cbd5e1; font-size: 14px;'>" . htmlspecialchars($e->getMessage()) . "</p>
|
||||||
|
<div style='background: #0b0f17; border: 1px solid #1f2937; padding: 15px; border-radius: 10px; font-size: 13px; margin-top: 15px;'>
|
||||||
Host: <strong>" . htmlspecialchars(DB_HOST) . "</strong><br>
|
Host: <strong>" . htmlspecialchars(DB_HOST) . "</strong><br>
|
||||||
Poort: <strong>" . htmlspecialchars(DB_PORT) . "</strong><br>
|
Poort: <strong>" . htmlspecialchars(DB_PORT) . "</strong><br>
|
||||||
Database: <strong>" . htmlspecialchars(DB_NAME) . "</strong><br>
|
Database: <strong>" . htmlspecialchars(DB_NAME) . "</strong><br>
|
||||||
Gebruiker: <strong>" . htmlspecialchars(DB_USER) . "</strong>
|
Gebruiker: <strong>" . htmlspecialchars(DB_USER) . "</strong>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
");
|
</div>
|
||||||
}
|
");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $pdo;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Zorgt ervoor dat alle tabellen uit schema.sql aanwezig zijn in MariaDB
|
* Zorgt ervoor dat alle benodigde tabellen uit schema.sql aanwezig zijn in MariaDB
|
||||||
|
* Voert niets uit als de tabellen al bestaan!
|
||||||
*/
|
*/
|
||||||
function ensure_tables_exist(PDO $pdo): void {
|
function ensure_tables_exist(PDO $pdo): void {
|
||||||
try {
|
try {
|
||||||
|
// Controleer of de hoofdtabel 'users' al bestaat
|
||||||
$result = $pdo->query("SHOW TABLES LIKE 'users'");
|
$result = $pdo->query("SHOW TABLES LIKE 'users'");
|
||||||
if ($result->rowCount() === 0) {
|
if ($result && $result->rowCount() > 0) {
|
||||||
$schemaFile = __DIR__ . '/../schema.sql';
|
// Tabellen bestaan al! Geen actie nodig.
|
||||||
if (file_exists($schemaFile)) {
|
return;
|
||||||
$sql = file_get_contents($schemaFile);
|
}
|
||||||
// Splits op puntkomma om elk CREATE TABLE statement apart uit te voeren
|
|
||||||
$statements = array_filter(array_map('trim', explode(';', $sql)));
|
// Tabellen ontbreken nog: voer schema.sql uit statement voor statement
|
||||||
foreach ($statements as $stmt) {
|
$schemaFile = __DIR__ . '/../schema.sql';
|
||||||
if (!empty($stmt)) {
|
if (file_exists($schemaFile)) {
|
||||||
$pdo->exec($stmt);
|
$sql = file_get_contents($schemaFile);
|
||||||
}
|
$statements = array_filter(array_map('trim', explode(';', $sql)));
|
||||||
|
foreach ($statements as $stmt) {
|
||||||
|
// Verwijder eventuele SQL commentaren aan het begin
|
||||||
|
$stmtClean = preg_replace('/^--.*$/m', '', $stmt);
|
||||||
|
$stmtClean = trim($stmtClean);
|
||||||
|
if (!empty($stmtClean)) {
|
||||||
|
$pdo->exec($stmtClean);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
error_log("Fout bij uitvoeren schema.sql: " . $e->getMessage());
|
error_log("Fout bij aanmaken tabellen: " . $e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user