-
Notifications
You must be signed in to change notification settings - Fork 0
/
the_connection.php
155 lines (95 loc) · 3.36 KB
/
the_connection.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
<?php
/*
* Author: Ian Innocent
* For: The Connection
*/
@session_start();
class connection{
public $db_name; public $db_host; public $db_username;
public $db_passwd; public $jsoncallback;
//The class constructor
public function __construct($db_name, $db_host, $db_username, $db_passwd, $jsoncallback){
$this->jsoncallback = $jsoncallback;
if(@$db_host != '' && @$db_name != '' && @$db_username != ''){
$this->db_host = @$db_host;
$this->db_name = @$db_name;
$this->db_passwd = @$db_passwd;
$this->db_username = @$db_username;
$this->db_connect();
}else{
$respArray = makeResponse( "ERROR", "Critical Error: Could recognize database connection criteria!" , "");
echo $this->jsoncallback."(".json_encode($respArray).")";
exit;
}
}
// The database connector!
public final function db_connect(){
$this->con = mysqli_connect($this->db_host,$this->db_username,$this->db_passwd,$this->db_name);
if (mysqli_connect_errno($this->con)){
$respArray = makeResponse( "ERROR", "Response: ".mysqli_connect_error(), "");
echo $this->jsoncallback."(".json_encode($respArray).")";
exit;
}
}
public function die_on_err($stops){
if(@$stops){
$_SESSION['query_error'] = array();
$_SESSION['query_error'][] = mysqli_error($this->con);
$respArray = makeResponse( "ERROR", "CRITICAL ERROR: ".mysqli_error($this->con), "");
echo $this->jsoncallback."(".json_encode($respArray).")";
exit;
}else{
if(!isset($_SESSION['query_error'])){
$_SESSION['query_error'] = array();
}
$_SESSION['query_error'][] = mysqli_error($this->con);
}
}
public function query($statement, $stops){
$_SESSION['query'] = mysqli_query($this->con,"$statement")or $this->die_on_err($stops);
}
public function aQuery($statement, $stops, $success, $failure, $scommand, $fcommand ){
$_SESSION['query'] = mysqli_query($this->con,"$statement")or $this->die_on_err($stops);
if($_SESSION['query']){
$respArray = makeResponse( "SUCCESS", $success , $scommand);
echo $this->jsoncallback."(".json_encode($respArray).")";
exit;
}else{
$respArray = makeResponse( "ERROR", $failure , $fcommand);
echo $this->jsoncallback."(".json_encode($respArray).")";
exit;
}
}
public function num_rows($statement, $stops){
$this->query($statement, $stops);
$stmt = $_SESSION['query'];
$_SESSION['num_rows'] = mysqli_num_rows( $stmt );
}
//SIMPLE RETURN RESULTS EXECUTOR
function printQueryResults($statement, $bool){
$this->query("$statement",$bool);
$elementArr = $_SESSION['query'];
$elements = array();
while($element = mysqli_fetch_array($elementArr)){
$elements[] = $element;
}
$respArray = makeResponse( "SUCCESS", $elements , "");
echo $this->jsoncallback."(".json_encode($respArray).")";
exit;
}
public function error_alert($status){
if(@$status){
if(@count($_SESSION['query_error']) > 0){
$err = $_SESSION['query_error'];
unset($_SESSION['query_error']);
return makeResponse("ERROR", $err, "" );
}else{
return;
}
}
}
private function makeResponse($response, $message, $command){
return array( "response" => $response, "data" => array( "message" => $message, "command" => $command ) );
}
//End of Class
}