-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosekaiDB.php
101 lines (92 loc) · 2.39 KB
/
osekaiDB.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
class Database
{
private static $db;
private $connection;
private function __construct()
{
$this->connection = new MySQLi('localhost', 'user', 'password', 'database');
$this->connection->set_charset("utf8mb4"); // to fix emojis in comments
}
function __destruct()
{
$this->connection->close();
}
public static function getConnection()
{
if (self::$db == null) {
self::$db = new Database();
}
return self::$db->connection;
}
/**
* @param string $strQuery
* @param string $strTypes
* @param array $colVariables
*
* @return array
*/
public static function execSelect($strQuery, $strTypes, $colVariables)
{
$mysql = self::getConnection();
$stmt = $mysql->prepare($strQuery);
$stmt->bind_param($strTypes, ...$colVariables);
$stmt->execute();
$meta = $stmt->result_metadata();
while ($field = $meta->fetch_field()) $params[] = &$row[$field->name];
$stmt->bind_result(...$params);
while ($stmt->fetch()) {
foreach ($row as $key => $val) {
$c[$key] = $val;
}
$hits[] = $c;
}
if ($mysql->more_results()) {
$mysql->next_result();
}
if (isset($hits)) {
return $hits;
} else {
return [];
}
return (array)$hits;
}
/**
* @param string $strQuery
*
* @return array
*/
public static function execSimpleSelect($strQuery)
{
$oQuery = self::getConnection()->query($strQuery);
while ($val = $oQuery->fetch_assoc()) {
$hits[] = $val;
}
return $hits;
}
/**
* @param string $strQuery
* @param string $strTypes
* @param array $colVariables
*
* @return void
*/
public static function execOperation($strQuery, $strTypes, $colVariables): void
{
$mysql = self::getConnection();
$stmt = $mysql->prepare($strQuery);
$stmt->bind_param($strTypes, ...$colVariables);
$stmt->execute();
}
/**
* @param string $strQuery
*
* @return void
*/
public static function execSimpleOperation($strQuery): void
{
$mysql = self::getConnection();
$stmt = $mysql->prepare($strQuery);
$stmt->execute();
}
}