-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.php
46 lines (41 loc) · 1.32 KB
/
connect.php
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
<?php
/**
* Connects to database
*
* @param array $config
* @return PDO
*/
function connect_database(array $config):PDO
{
// Gets the database type
$database = $config['database'] ?? null;
if (is_null($database)) throw new Exception('Can not connect');
// Prepares the database file such as SQLite
if (isset($config['connections'][$database]['db_file']))
{
$db_file = $config['connections'][$database]['db_file'];
if (!file_exists($db_path=dirname($db_file)))
{
@mkdir($db_path, null, true);
}
}
//-------------------------------------------
// Connects to database
//-------------------------------------------
$dsn = $config['connections'][$database]['dsn'];
$username = $config['connections'][$database]['username'] ?? null;
$password = $config['connections'][$database]['password'] ?? null;
$options = $config['connections'][$database]['driver_options'] ?? null;
$db = new PDO($dsn, $username, $password, $options);
// Executes initial SQL statements
$initial_statements = $config['connections'][$database]['initial_statements'] ?? null;
if (isset($initial_statements))
{
foreach((array)$initial_statements as $sql)
{
$db->exec($sql);
}
}
// return PDO object
return $db;
};