-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataModel.php
executable file
·100 lines (87 loc) · 2.54 KB
/
DataModel.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
function reverse_escape($str) {
$search = [ "\\\\" , "\\0" , "\\n" , "\\r" , "\Z" , "\'" , '\"' ];
$replace = [ "\\" , "\0" , "\n" , "\r" , "\x1a" , "'" , '"' ];
return str_replace($search, $replace, $str);
}
function stripslashes_deep($value) {
return is_array($value)
? array_map('reverse_escape', $value)
: reverse_escape($value);
}
function getMainDB() {
global $maindb;
if (!$maindb) {
$maindb = new mysqli(DBHOST, DBUSER, DBPASSWD, DBNAME);
$maindb->set_charset('utf8mb4');
}
}
getMainDB();
abstract class DataModel {
public function getHelperByName($name) {
$class = ucfirst($name) . 'Helper';
$helperfile = HELPER_DIR . '/' . $class . '.php';
include_once $helperfile;
return new $class;
}
public function mysql_fetch_all($result) {
$return = [];
while ($row = $result->fetch_array(MYSQL_ASSOC)) {
$return[] = stripslashes_deep($row);
}
return $return;
}
public function query($sql) {
global $maindb;
$maindb->query($sql);
if ($maindb->error) {
error_log("SQL error: {$maindb->error}\nSQL: {$sql}");
return null;
}
$result = [];
$insert_id = $maindb->insert_id;
if ($insert_id > 0) {
$result['insert_id'] = strval($insert_id);
}
$result['affected_rows'] = $maindb->affected_rows;
return $result;
}
public function getAll($sql) {
global $maindb;
return ($query = $maindb->query($sql))
? $this->mysql_fetch_all($query)
: null;
}
public function getRow($sql) {
global $maindb;
if (($query = $maindb->query($sql))) {
if (($return = $query->fetch_array(MYSQL_ASSOC))) {
return $return;
}
}
}
public function countNum($sql) {
$row = $this->getRow($sql);
if (!empty($row)) {
foreach ($row as $key => $value) {
return $value;
}
}
return 0;
}
public function getColumn($sql) {
global $maindb;
$result = [];
if (($query = $maindb->query($sql))) {
if (($data = $this->mysql_fetch_all($query))) {
foreach ($data as $row) {
foreach ($row as $name => $value) {
$result[] = $value;
}
}
}
return $result;
}
return null;
}
}