-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
34 lines (26 loc) · 652 Bytes
/
db.php
File metadata and controls
34 lines (26 loc) · 652 Bytes
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
<?php
function dbInstance() : PDO{
static $db;
if($db === null){
$db = new PDO('mysql:host=localhost;dbname=' . DB_NAME, DB_LOGIN, DB_PASS, [
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
$db->exec('SET NAMES UTF8');
}
return $db;
}
function dbQuery(string $sql, array $params = []) : PDOStatement{
$db = dbInstance();
$query = $db->prepare($sql);
$query->execute($params ?? []);
dbCheckError($query);
return $query;
}
function dbCheckError(PDOStatement $query) : bool{
$errInfo = $query->errorInfo();
if($errInfo[0] !== PDO::ERR_NONE){
echo $errInfo[2];
exit();
}
return true;
}