forked from deaamaliaputri/laundry_online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.php
315 lines (293 loc) · 10.7 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
# MySQLi Class PHP
# @Package Database PHP
# @since 2.5
# @Version 1.3
class Database {
/*
* Create variables for credentials to MySQL database
* The variables have been declared as private. This
* means that they will only be available with the
* Database class
*/
private $db_host = "localhost"; // Change as required
private $db_user = "root"; // Change as required
private $db_pass = ""; // Change as required
private $db_name = "11_deaamaliaputri_laundry"; // Change as required
/*
* Extra variables that are required by other function such as boolean con variable
*/
var $con = false; // Check to see if the connection is active
var $myconn = ""; // This will be our mysqli object
var $result = array(); // Any results from a query will be stored here
var $myQuery = ""; // used for debugging process with SQL return
var $numResults = ""; // used for returning the number of rows
function __construct() {
/* Connect to the MySQli Server */
if(!$this->con){
$this->myconn = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name); // mysql_connect() with variables defined at the start of Database class
if($this->myconn->connect_errno > 0){
array_push($this->result, $this->myconn->connect_error);
return false; // Problem selecting database return FALSE
}else{
$this->myconn->set_charset('utf8'); // change character set to utf8
$this->con = true;
return true; // Connection has been made return TRUE
}
} else {
return true; // Connection has already been made return TRUE
}
}
function __destruct() {
// If there is a connection to the database
if($this->con){
// We have found a connection, try to close it
if($this->myconn->close()){
// We have successfully closed the connection, set the connection variable to false
$this->con = false;
// Return true tjat we have closed the connection
return true;
}else{
// We could not close the connection, return false
return false;
}
}
}
public function query($sql){
$query = $this->myconn->query($sql);
$this->myQuery = $sql; // Pass back the SQL
if($query){
// If the query returns >= 1 assign the number of rows to numResults
$this->numResults = $query->num_rows;
// Loop through the query results by the number of rows returned
for($i = 0; $i < $this->numResults; $i++){
$r = $query->fetch_array();
$key = array_keys($r);
for($x = 0; $x < count($key); $x++){
// Sanitizes keys so only alphavalues are allowed
if(!is_int($key[$x])){
if($query->num_rows >= 1){
$this->result[$i][$key[$x]] = $r[$key[$x]];
}else{
$this->result = null;
}
}
}
}
return true; // Query was successful
}else{
array_push($this->result,$this->myconn->error);
return false; // No rows where returned
}
}
// Function to SELECT from the database
public function select($table, $rows = '*', $join = null,$join2 = null , $where = null, $order = null, $limit = null){
// Create query from the variables passed to the function
$q = 'SELECT '.$rows.' FROM '.$table;
if($join != null){
$q .= ' JOIN '.$join;
}
if($join2 != null){
$q .= ' JOIN '.$join2;
}
if($where != null){
$q .= ' WHERE '.$where;
}
if($order != null){
$q .= ' ORDER BY '.$order;
}
if($limit != null){
$q .= ' LIMIT '.$limit;
}
// echo $table;
$this->myQuery = $q; // Pass back the SQL
// Check to see if the table exists
if($this->tableExists($table)){
// The table exists, run the query
$query = $this->myconn->query($q);
if($query){
// If the query returns >= 1 assign the number of rows to numResults
$this->numResults = $query->num_rows;
// Loop through the query results by the number of rows returned
for($i = 0; $i < $this->numResults; $i++){
$r = $query->fetch_array();
$key = array_keys($r);
for($x = 0; $x < count($key); $x++){
// Sanitizes keys so only alphavalues are allowed
if(!is_int($key[$x])){
if($query->num_rows >= 1){
$this->result[$i][$key[$x]] = $r[$key[$x]];
}else{
$this->result[$i][$key[$x]] = null;
}
}
}
}
return true; // Query was successful
}else{
array_push($this->result, $this->myconn->error);
return false; // No rows where returned
}
}else{
return false; // Table does not exist
}
}
public function selectMax($table, $field) {
$q = 'SELECT MAX('.$field.') as max FROM '.$table;
// echo $table;
$this->myQuery = $q; // Pass back the SQL
// Check to see if the table exists
if($this->tableExists($table)){
// The table exists, run the query
$query = $this->myconn->query($q);
if($query){
// If the query returns >= 1 assign the number of rows to numResults
$this->numResults = $query->num_rows;
// Loop through the query results by the number of rows returned
for($i = 0; $i < $this->numResults; $i++){
$r = $query->fetch_array();
$key = array_keys($r);
for($x = 0; $x < count($key); $x++){
// Sanitizes keys so only alphavalues are allowed
if(!is_int($key[$x])){
if($query->num_rows >= 1){
$this->result[$i][$key[$x]] = $r[$key[$x]];
}else{
$this->result[$i][$key[$x]] = null;
}
}
}
}
return true; // Query was successful
}else{
array_push($this->result, $this->myconn->error);
return false; // No rows where returned
}
}else{
return false; // Table does not exist
}
}
// Function to insert into the database
public function insert($table, $params=array()){
// Check to see if the table exists
if($this->tableExists($table)){
$sql='INSERT INTO `'.$table.'` (`'.implode('`, `',array_keys($params)).'`) VALUES ("' . implode('", "', $params) . '")';
$this->myQuery = $sql; // Pass back the SQL
// Make the query to insert to the database
if($ins = $this->myconn->query($sql)){
array_push($this->result, $this->myconn->insert_id);
return true; // The data has been inserted
}else{
array_push($this->result, $this->myconn->error);
return false; // The data has not been inserted
}
}else{
return false; // Table does not exist
}
}
//Function to delete table or row(s) from database
public function delete($table, $where = null){
// Check to see if table exists
if($this->tableExists($table)){
// The table exists check to see if we are deleting rows or table
if($where == null){
$delete = 'DELETE '.$table; // Create query to delete table
}else{
$delete = 'DELETE FROM '.$table.' WHERE '.$where; // Create query to delete rows
}
// Submit query to database
if($del = $this->myconn->query($delete)){
array_push($this->result, $this->myconn->affected_rows);
$this->myQuery = $delete; // Pass back the SQL
return true; // The query exectued correctly
}else{
array_push($this->result, $this->myconn->error);
return false; // The query did not execute correctly
}
}else{
return false; // The table does not exist
}
}
// Function to update row in database
public function update($table, $params=array(), $where){
// Check to see if table exists
if($this->tableExists($table)){
// Create Array to hold all the columns to update
$args=array();
foreach($params as $field=>$value){
// Seperate each column out with it's corresponding value
$args[]=$field.'="'.$value.'"';
}
// Create the query
$sql='UPDATE '.$table.' SET '.implode(',',$args).' WHERE '.$where;
// Make query to database
$this->myQuery = $sql; // Pass back the SQL
if($query = $this->myconn->query($sql)){
array_push($this->result, $this->myconn->affected_rows);
return true; // Update has been successful
}else{
array_push($this->result, $this->myconn->error);
return false; // Update has not been successful
}
}else{
return false; // The table does not exist
}
}
// Private function to check if table exists for use with queries
private function TableExists($table) {
$res = $this->myconn->query('SHOW TABLES LIKE "'.$table.'"');
if(isset($res->num_rows)) {
return $res->num_rows > 0 ? true : false;
} else {
array_push($this->result, $table." does not exist in this database");
return false;
}
}
// Public function to return the data to the user
public function getResult(){
$val = $this->result;
$this->result = array();
return $val;
}
public function getOne(){
$val = $this->result;
return $val;
}
//Pass the SQL back for debugging
public function getQuery(){
$val = $this->myQuery;
$this->myQuery = array();
return $val;
}
//Pass the number of rows back
public function numRows(){
$val = $this->numResults;
$this->numResults = array();
return $val;
}
// Escape your string
public function escapeString($data){
return $this->myconn->real_escape_string($data);
}
function insert_id(){
return $this->myconn->insert_id;
}
function free($query) {
$this->myconn->free_result($query);
}
function close() {
// If there is a connection to the database
if($this->con){
// We have found a connection, try to close it
if($this->myconn->close()){
// We have successfully closed the connection, set the connection variable to false
$this->con = false;
// Return true tjat we have closed the connection
return true;
}else{
// We could not close the connection, return false
return false;
}
}
}
}