Fixes error in database selection

main
Yasen Pramatarov 2024-08-10 21:57:41 +03:00
parent aeb837fee5
commit e6d6a10795
5 changed files with 49 additions and 31 deletions

View File

@ -1,43 +1,61 @@
<?php <?php
// connect to database // connect to database
function connectDB($config) { function connectDB($config,$database) {
// sqlite database file
if ($config['db_type'] === 'sqlite') { if ($database === 'jilo') {
try { try {
$db = new Database([ $db = new Database([
'type' => $config['db_type'], 'type' => 'sqlite',
'dbFile' => $config['sqlite_file'], 'dbFile' => $config['jilo_database'],
]); ]);
$pdo = $db->getConnection();
} catch (Exception $e) { } catch (Exception $e) {
$error = 'Error: ' . $e->getMessage(); $error = 'Error: ' . $e->getMessage();
include 'templates/block-message.php'; include 'templates/block-message.php';
exit(); exit();
} }
// mysql/mariadb database
} elseif ($config['db_type'] === 'mysql' || $config['db_type'] === 'mariadb') {
try {
$db = new Database([
'type' => $config['db_type'],
'host' => $config['sql_host'] ?? 'localhost',
'port' => $config['sql_port'] ?? '3306',
'dbname' => $config['sql_database'],
'user' => $config['sql_username'],
'password' => $config['sql_password'],
]);
$pdo = $db->getConnection();
} catch (Exception $e) {
$error = 'Error: ' . $e->getMessage();
include 'templates/block-message.php';
exit();
}
// unknown database
} else { } else {
$error = "Error: unknow database type \"{$config['db_type']}\"";
include 'templates/block-message.php'; // sqlite database file
exit(); if ($config['db_type'] === 'sqlite') {
try {
$db = new Database([
'type' => $config['db_type'],
'dbFile' => $config['sqlite_file'],
]);
$pdo = $db->getConnection();
} catch (Exception $e) {
$error = 'Error: ' . $e->getMessage();
include 'templates/block-message.php';
exit();
}
// mysql/mariadb database
} elseif ($config['db_type'] === 'mysql' || $config['db_type'] === 'mariadb') {
try {
$db = new Database([
'type' => $config['db_type'],
'host' => $config['sql_host'] ?? 'localhost',
'port' => $config['sql_port'] ?? '3306',
'dbname' => $config['sql_database'],
'user' => $config['sql_username'],
'password' => $config['sql_password'],
]);
$pdo = $db->getConnection();
} catch (Exception $e) {
$error = 'Error: ' . $e->getMessage();
include 'templates/block-message.php';
exit();
}
// unknown database
} else {
$error = "Error: unknow database type \"{$config['db_type']}\"";
include 'templates/block-message.php';
exit();
}
} }
return $db; return $db;
} }
?> ?>

View File

@ -5,7 +5,7 @@ require 'classes/component.php';
// connect to database // connect to database
require 'helpers/database.php'; require 'helpers/database.php';
$db = connectDB($config); $db = connectDB($config, 'jilo');
// FIXME move thi sto a special function // FIXME move thi sto a special function
$time_range_specified = false; $time_range_specified = false;

View File

@ -5,7 +5,7 @@ require 'classes/conference.php';
// connect to database // connect to database
require 'helpers/database.php'; require 'helpers/database.php';
$db = connectDB($config); $db = connectDB($config, 'jilo');
// FIXME move thi sto a special function // FIXME move thi sto a special function
$time_range_specified = false; $time_range_specified = false;

View File

@ -6,7 +6,7 @@ require 'classes/participant.php';
// connect to database // connect to database
require 'helpers/database.php'; require 'helpers/database.php';
$db = connectDB($config); $db = connectDB($config, 'jilo');
// //

View File

@ -5,7 +5,7 @@ require 'classes/participant.php';
// connect to database // connect to database
require 'helpers/database.php'; require 'helpers/database.php';
$db = connectDB($config); $db = connectDB($config, 'jilo');
// FIXME move thi sto a special function // FIXME move thi sto a special function
$time_range_specified = false; $time_range_specified = false;