-
Notifications
You must be signed in to change notification settings - Fork 0
/
getters.php
104 lines (93 loc) · 2.5 KB
/
getters.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
101
102
103
104
<?php
/**
* Gets a single resource from the database, given a table and value with the default column matched being id.
*
* @param string $table
* @param mixed $value
* @param string $key, default = 'id'
* @return mixed
*/
function get($table, $value, $key = 'id') {
global $mysqli;
if ($result = $mysqli->query("SELECT * FROM $table WHERE $key = '$value'")) {
return $result->fetch_assoc();
}
return false;
}
/**
* Returns an array of resources from the database.
*
* @param string $table
* @param integer $limit, default = 1000
* @return array $results
*/
function getMany($table, $limit = 1000) {
global $mysqli;
$results = [];
if ($result = $mysqli->query("SELECT * FROM $table LIMIT $limit")) {
while ($each = $result->fetch_assoc()) {
array_push($results, $each);
}
return $results;
}
return [];
}
/**
* Gets many resources from the database matching WHERE criteria.
*
* @param string $table
* @param string $key
* @param mixed $value
* @param integer $limit, default = 1000
* @return array $results
*/
function getManyWhere($table, $key, $value, $limit = 1000) {
global $mysqli;
$results = [];
if ($result = $mysqli->query("SELECT * FROM $table WHERE $key = $value LIMIT $limit")) {
while ($each = $result->fetch_assoc()) {
array_push($results, $each);
}
return $results;
}
return [];
}
/**
* Get many resources from the database ordered by most recent.
*
* @param string $table
* @param integer $limit, default = 1000
* @return array $results
*/
function getRecent($table, $limit = 1000) {
global $mysqli;
$results = [];
if ($result = $mysqli->query("SELECT * FROM $table ORDER BY created_at desc LIMIT $limit")) {
while ($each = $result->fetch_assoc()) {
array_push($results, $each);
}
return $results;
}
return [];
}
/**
* Get many resources from the database ordered by most recent matchign WHERE criteria.
*
* @param string $table
* @param string $key
* @param mixed $value
* @param integer $limit, default = 1000
* @return array $results
*/
function getRecentWhere($table, $key, $value, $limit = 1000) {
global $mysqli;
$results = [];
if ($result = $mysqli->query("SELECT * FROM $table WHERE $key = $value ORDER BY created_at desc LIMIT $limit")) {
while ($each = $result->fetch_assoc()) {
array_push($results, $each);
}
return $results;
}
return [];
}
?>