-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.php
119 lines (96 loc) · 4.19 KB
/
database.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) { header( "HTTP/1.1 404 Not Found"); die();} ?>
<?php
class DB {
private $limit = 0;
private $query, $result, $con;
public function __construct() {
/** @var string $location */
/** @var string $username */
/** @var string $password */
/** @var string $database */
require __DIR__ . '/config.php';
$this->con = mysqli_connect($location, $username, $password ,$database);
if(!$this->con) {
echo 'Cannot establish database connection';
die();
}
}
public function fire($query) {
$this->query = $query;
$this->addLimit(); // Adds the limit if set by user
$this->result = mysqli_query($this->con, $this->query); // Fires the actuall query into MYSQL
if(!$this->result) {
$this->printError();
return false; // Returns false if query failed to execute
}
if($this->getQueryType() == 'SELECT') $this->result = $this->fetchAllRows(); // Store an array in result variable if query is of type SELECT
return $this->result;
}
public function insert($table, $fields) {
$keys = '(`' . implode('`,`', array_keys($fields)) . '`)';
$values = "('" . implode("','", $this->escapeArray(array_values($fields))) . "')";
$query = "INSERT INTO `$table` " . $keys . ' VALUES ' . $values;
return $this->fire($query);
}
public function select($table, $fields = []) {
if($fields) {
$list = $this->setSQLString($fields);
return $this->fire("SELECT * FROM $table WHERE " . $list);
} else {
return $this->fire("SELECT * FROM $table");
}
}
public function fetch($table, $fields = []) { return $this->select($table, $fields); }
public function update($table, $match, $fields) {
$match = $this->setSQLString($match);
$fields = $this->setSQLString($fields, ",");
return $this->fire("UPDATE $table SET " . $fields . " WHERE " . $match);
}
public function delete($table, $fields) {
$list = $this->setSQLString($fields);
return $this->fire("DELETE FROM $table WHERE " . $list);
}
public function getNumRows() { return count($this->result); }
public function getSingleRow() { return $this->result[0]; }
public function getAllRows() { return $this->result; }
public function getError() { return mysqli_error($this->con); }
public function getQuery() { return $this->query; }
public function affectedRows() { return $this->con->affected_rows; }
private function fetchAllRows() {
$rows = [];
while($row = mysqli_fetch_assoc($this->result)) {
array_push($rows,$row);
}
return $rows;
}
private function printError() {
if(ini_get('display_errors'))
echo "query => $this->query<br>\nerror => " . $this->getError();
}
private function getQueryType() { return strtoupper(explode(' ', $this->query)[0]); }
private function addLimit() {
$this->query = $this->limit ? $this->query . ' LIMIT ' . $this->limit : $this->query;
$this->limit = 0;
}
private function setSQLString(array $array, string $separator = 'AND') {
return implode(" $separator ", array_map(function ($key, $value) {
if(is_array($value)) {
return '(' . implode(' OR ' , array_map(function ($value) use ($key) {
if(is_array($value)) {
return "(`$key` >= '$value[0]' AND '$value[1]' >= `$key`)";
} else {
return "`$key` = '$value'";
}
}, $this->escapeArray($value))) . ')';
} else {
return "`$key` = '$value'";
}
}, array_keys($array), $this->escapeArray(array_values($array))));
}
private function escapeArray(array $array) {
return array_map(function ($item) {
if(is_array($item)) return $item;
return mysqli_real_escape_string($this->con, $item);
}, $array);
}
}