-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.php
More file actions
56 lines (46 loc) · 1.58 KB
/
Copy pathdatabase.php
File metadata and controls
56 lines (46 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* Secure Database Connection Helper
* Uses configuration from config.php
*/
function getDatabaseConnection() {
// Load configuration
$config_file = __DIR__ . '/config.php';
if (!file_exists($config_file)) {
throw new Exception('Configuration file not found. Please copy config.example.php to config.php and update your credentials.');
}
$config = require $config_file;
$db_config = $config['database'];
try {
$conn = new mysqli(
$db_config['host'],
$db_config['username'],
$db_config['password'],
$db_config['database']
);
// Set charset
$conn->set_charset($db_config['charset'] ?? 'utf8mb4');
if ($conn->connect_error) {
throw new Exception('Database connection failed: ' . $conn->connect_error);
}
return $conn;
} catch (Exception $e) {
error_log('Database connection error: ' . $e->getMessage());
throw new Exception('Database connection failed. Please check your configuration.');
}
}
function getConfig($section = null) {
static $config = null;
if ($config === null) {
$config_file = __DIR__ . '/config.php';
if (!file_exists($config_file)) {
throw new Exception('Configuration file not found. Please copy config.example.php to config.php and update your credentials.');
}
$config = require $config_file;
}
if ($section === null) {
return $config;
}
return $config[$section] ?? null;
}
?>