-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.php
135 lines (129 loc) · 3.33 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
<?php
/* This class has not been updated
* look into how the cxn are done!
*
*
*
*/
class Database{
protected $cxn;
public $results, $numRows;
function __construct(array $cxnData = NULL) {
$this->results = NULL;
$this->cxn = NULL;
$this->numRows = 0;
if(!is_null($cxnData)){
$this->setCxn($cxnData);
} else if($this->_isCxnStored()){
$this->cxn = &$this->_getStoredCxn();
} else {
$this->cxn = null;
}
}
public function setCxn(array $data, $store = true){
try {
$this->_createCxn($data['host'], $data['user'], $data['pass'], $data['db']);
if($store) $this->_storeCxn();
} catch (Exception $e){
$this->log($e->getMessage());
}
}
public function getLastIndex(){
return $this->cxn->insert_id;
}
protected function log($message){
echo "<br>Log: ".$message;
}
// ------------------ Run Query ------------------------------------------------
public function runQuery($query){
$this->log($query);
if ($this->cxn){
$this->results = $this->cxn->query($query);
if (!$this->results) {
$this->log("FAIL");
$this->log('Invalid query: ' . $this->cxn->error . "\n");
$this->log('<br>Whole query: ' . $query);
return FALSE;
}
$this->numRows = $this->results->num_rows;
return TRUE;
}
}
public function runUpdateQuery($query){
//$query = $this->cxn->real_escape_string($query);
$this->log($query);
if ($this->cxn){
$this->results = $this->cxn->query($query);
if (!$this->results) {
$this->log("FAIL");
$this->log('Invalid query: ' . $this->cxn->error . "\n");
$this->log('<br>Whole query: ' . $query);
return FALSE;
}
$this->numRows = $this->cxn->affected_rows;
return TRUE;
}
}
//--------------------- Get Results Functions ------------------------
public function getResults(){
if(!$this->results)
return false;
else
return $this->results;
}
public function getResultsAssoc(){
if(!$this->results)
return false;
$data = array();
while ($row = $this->results->fetch_assoc()){
$data[] = $row;
}
return $data;
}
public function getResultsIndex(){
if(!$this->results)
return false;
$data = array();
while ($row = $this->results->fetch_row()){
$data[] = $row;
}
return $data;
}
public function getFirstResult() {
if(!$this->results)
return false;
$firstResult = $this->results->fetch_assoc();
return $firstResult;
}
//----------------------- PRIVATE FUNCTIONS --------------------------------------------
private function _createCxn($HOST, $USER, $PASS, $DB){
try {
$this->cxn = new mysqli($HOST, $USER, $PASS, $DB );
} catch (exception $e){
echo $e->getMessage();
}
if (!$this->cxn) {
$this->message = 'Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error();
return FALSE;
} else return TRUE;
}
private function closeCxn(){
$this->cxn->close();
}
private function & _getCxn(){
return $this->cxn;
}
protected function _storeCxn(){
$GLOBALS['Database']['cxn'] = & $this->cxn;
}
protected function &_getStoredCxn(){
if($this->_isCxnStored())
return $GLOBALS['Database']['cxn'];
else
return false;
}
protected function _isCxnStored(){
return isset($GLOBALS['Database']['cxn']);
}
}
?>