-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseManager.class.php
More file actions
71 lines (54 loc) · 1.64 KB
/
DatabaseManager.class.php
File metadata and controls
71 lines (54 loc) · 1.64 KB
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
<?php
//HANDLES MYSQL QUERIES
class DatabaseManager {
private $con = NULL;
public function __construct() {
$this->con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die("{\"fail\" : \"Could not connect to MySQL: ". mysql_error() . "\"}");
if(mysqli_connect_errno($this->con)) {
echo json_encode(array("fail" => "Connect failed: " . mysqli_connect_error()));
exit(0);
}
}
public function query($query, $params) {
$retval = 0;
if($stmt = $this->con->prepare($query)) {
call_user_func_array( array($stmt, 'bind_param'), $this->refValues($params));
if($stmt->execute()) {
$retval = $stmt->affected_rows;
}
$stmt->close();
} else {
die(json_encode(array("fail" => "There was an error with the prepared statement: " . $this->con->error)));
}
return $retval;
}
public function &queryForResult($query, $params) {
$result = FALSE;
if($stmt = $this->con->prepare($query)) {
if(count($params) > 1) {
call_user_func_array( array($stmt, 'bind_param'), $this->refValues($params));
}
if($stmt->execute()) {
$result = $stmt;
} else {
$stmt->close();
}
} else {
die(json_encode(array("fail" => "There was an error with the prepared statement: " . $this->con->error)));
}
return $result;
}
private function refValues($arr) {
$refs = array();
foreach ($arr as $key => $value) {
$refs[$key] = &$arr[$key];
}
return $refs;
}
public function __destruct() {
if($this->con) {
$this->con->close();
}
}
}
?>