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
// connect to database
function connectDB($config) {
// sqlite database file
if ($config['db_type'] === 'sqlite') {
function connectDB($config,$database) {
if ($database === 'jilo') {
try {
$db = new Database([
'type' => $config['db_type'],
'dbFile' => $config['sqlite_file'],
'type' => 'sqlite',
'dbFile' => $config['jilo_database'],
]);
$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();
// sqlite database file
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;
}
?>

View File

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

View File

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

View File

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

View File

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