jilo-web/public_html/helpers/database.php

62 lines
1.9 KiB
PHP
Raw Normal View History

2024-08-10 18:42:44 +00:00
<?php
// connect to database
2024-08-10 18:57:41 +00:00
function connectDB($config,$database) {
if ($database === 'jilo') {
2024-08-10 18:42:44 +00:00
try {
$db = new Database([
2024-08-10 18:57:41 +00:00
'type' => 'sqlite',
'dbFile' => $config['jilo_database'],
2024-08-10 18:42:44 +00:00
]);
} catch (Exception $e) {
$error = 'Error: ' . $e->getMessage();
include 'templates/block-message.php';
exit();
}
2024-08-10 18:57:41 +00:00
} else {
// 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']}\"";
2024-08-10 18:42:44 +00:00
include 'templates/block-message.php';
exit();
}
2024-08-10 18:57:41 +00:00
2024-08-10 18:42:44 +00:00
}
2024-08-10 18:57:41 +00:00
2024-08-10 18:42:44 +00:00
return $db;
}
?>