This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection.php
47 lines (35 loc) · 1.56 KB
/
connection.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
47
<?php
function getDBConnection(string $db_name, string $schema = "sqlite") : PDO {
try {
static $dbMapping = array();
$connectionString = $schema.':'.$db_name;
if (!array_key_exists($connectionString, $dbMapping)) {
$db = new PDO($connectionString);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("PRAGMA foreign_keys = ON");
$dbMapping[$connectionString] = $db;
}
return $dbMapping[$connectionString];
} catch (PDOException $exception) {
die("Error connecting to DB: ".$exception->getMessage());
}
}
function getQueryResults(PDO $db, string $query, bool $fetchMultiple = true, array $params = null) : array | false {
try {
list($result, $stmt) = executeQuery($db, $query, $params);
if ($result)
return $fetchMultiple ? $stmt->fetchAll() : $stmt->fetch();
} catch (PDOException $e) {
} // do nothing and leave block, expected behavior is to return false
return false;
}
function executeQuery(PDO $db, string $query, array $params = null): array {
try {
if ($stmt = $db->prepare($query))
return array($stmt->execute($params), $stmt);
} catch (PDOException $e) {
} // do nothing and leave block, expected behavior is to return false
return array(false, null);
}
?>