-
Notifications
You must be signed in to change notification settings - Fork 9
/
Database.class.php
253 lines (202 loc) · 7.35 KB
/
Database.class.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
<?php
/*!
* **************************************************************
**************** ProQuiz V2.0.0b ******************************
***************************************************************/
/* documentation at: http://proquiz.softon.org/documentation/
/* Designed & Maintained by
/* - Softon Technologies
/* Developer
/* - Manzovi
/* For Support Contact @
/* - proquiz@softon.org
/* version 2.0.0 beta (2 Feb 2011)
/* Licensed under GPL license:
/* http://www.gnu.org/licenses/gpl.html
*/
?><?php
class Database {
var $server = "localhost"; //database server
var $user = "root"; //database login name
var $pass = ""; //database login password
var $database = "userauth"; //database name
var $pre = ""; //table prefix
#######################
//internal info
var $error = "";
var $errno = 0;
//number of rows affected by SQL query
var $affected_rows = 0;
var $link_id = 0;
var $query_id = 0;
var $debug = 0;
#-#############################################
# desc: constructor
function Database($server, $user, $pass, $database, $pre=''){
$this->server=$server;
$this->user=$user;
$this->pass=$pass;
$this->database=$database;
$this->pre=$pre;
}#-#constructor()
#-#############################################
# desc: connect and select database using vars above
# Param: $new_link can force connect() to open a new link, even if mysql_connect() was called before with the same parameters
function connect($new_link=false) {
$this->link_id=@mysql_connect($this->server,$this->user,$this->pass,$new_link);
if (!$this->link_id) {//open failed
$this->oops("Could not connect to server: <b>$this->server</b>.");
}
if(!@mysql_select_db($this->database, $this->link_id)) {//no database
$this->oops("Could not open database: <b>$this->database</b>.");
}
// unset the data so it can't be dumped
$this->server='';
$this->user='';
$this->pass='';
$this->database='';
}#-#connect()
#-#############################################
# desc: close the connection
function close() {
if(!@mysql_close($this->link_id)){
$this->oops("Connection close failed.");
}
}#-#close()
#-#############################################
# Desc: escapes characters to be mysql ready
# Param: string
# returns: string
function escape($string) {
if(get_magic_quotes_runtime()) $string = stripslashes($string);
return @mysql_real_escape_string($string,$this->link_id);
}#-#escape()
#-#############################################
# Desc: executes SQL query to an open connection
# Param: (MySQL query) to execute
# returns: (query_id) for fetching results etc
function query($sql) {
// Print Query For Debug
if ($debug==1) {
$this->oops("<b>MySQL Query Executed:</b> $sql");
}
// do query
$this->query_id = @mysql_query($sql, $this->link_id);
if (!$this->query_id) {
$this->oops("<b>MySQL Query fail:</b> $sql");
return 0;
}
$this->affected_rows = @mysql_affected_rows($this->link_id);
return $this->query_id;
}#-#query()
#-#############################################
# desc: fetches and returns results one line at a time
# param: query_id for mysql run. if none specified, last used
# return: (array) fetched record(s)
function fetch_array($query_id=-1) {
// retrieve row
if ($query_id!=-1) {
$this->query_id=$query_id;
}
if (isset($this->query_id)) {
$record = @mysql_fetch_assoc($this->query_id);
}else{
$this->oops("Invalid query_id: <b>$this->query_id</b>. Records could not be fetched.");
}
return $record;
}#-#fetch_array()
#-#############################################
# desc: returns all the results (not one row)
# param: (MySQL query) the query to run on server
# returns: assoc array of ALL fetched results
function fetch_all_array($sql) {
$query_id = $this->query($sql);
$out = array();
while ($row = $this->fetch_array($query_id)){
$out[] = $row;
}
$this->free_result($query_id);
return $out;
}#-#fetch_all_array()
#-#############################################
# desc: frees the resultset
# param: query_id for mysql run. if none specified, last used
function free_result($query_id=-1) {
if ($query_id!=-1) {
$this->query_id=$query_id;
}
if($this->query_id!=0 && !@mysql_free_result($this->query_id)) {
$this->oops("Result ID: <b>$this->query_id</b> could not be freed.");
}
}#-#free_result()
#-#############################################
# desc: does a query, fetches the first row only, frees resultset
# param: (MySQL query) the query to run on server
# returns: array of fetched results
function query_first($query_string) {
$query_id = $this->query($query_string);
$out = $this->fetch_array($query_id);
$this->free_result($query_id);
return $out;
}#-#query_first()
#-#############################################
# desc: does an update query with an array
# param: table (no prefix), assoc array with data (doesn't need escaped), where condition
# returns: (query_id) for fetching results etc
function query_update($table, $data, $where='1') {
$q="UPDATE `".$this->pre.$table."` SET ";
foreach($data as $key=>$val) {
if(strtolower($val)=='null') $q.= "`$key` = NULL, ";
elseif(strtolower($val)=='now()') $q.= "`$key` = NOW(), ";
elseif(preg_match("/^increment\((\-?\d+)\)$/i",$val,$m)) $q.= "`$key` = `$key` + $m[1], ";
else $q.= "`$key`='".$this->escape($val)."', ";
}
$q = rtrim($q, ', ') . ' WHERE '.$where.';';
return $this->query($q);
}#-#query_update()
#-#############################################
# desc: does an insert query with an array
# param: table (no prefix), assoc array with data
# returns: id of inserted record, false if error
function query_insert($table, $data) {
$q="INSERT INTO `".$this->pre.$table."` ";
$v=''; $n='';
foreach($data as $key=>$val) {
$n.="`$key`, ";
if(strtolower($val)=='null') $v.="NULL, ";
elseif(strtolower($val)=='now()') $v.="NOW(), ";
else $v.= "'".$this->escape($val)."', ";
}
$q .= "(". rtrim($n, ', ') .") VALUES (". rtrim($v, ', ') .");";
if($this->query($q)){
//$this->free_result();
return mysql_insert_id($this->link_id);
}
else return false;
}#-#query_insert()
#-#############################################
# desc: throw an error message
# param: [optional] any custom error to display
function oops($msg='') {
if($this->link_id>0){
$this->error=mysql_error($this->link_id);
$this->errno=mysql_errno($this->link_id);
}
else{
$this->error=mysql_error();
$this->errno=mysql_errno();
}
?>
<table align="center" border="1" cellspacing="0" style="background:white;color:black;width:80%;">
<tr><th colspan=2>Database Error</th></tr>
<tr><td align="right" valign="top">Message:</td><td><?php echo $msg; ?></td></tr>
<?php if(!empty($this->error)) echo '<tr><td align="right" valign="top" nowrap>MySQL Error:</td><td>'.$this->error.'</td></tr>'; ?>
<tr><td align="right">Date:</td><td><?php echo date("l, F j, Y \a\\t g:i:s A"); ?></td></tr>
<?php if(!empty($_SERVER['REQUEST_URI'])) echo '<tr><td align="right">Script:</td><td><a href="'.$_SERVER['REQUEST_URI'].'">'.$_SERVER['REQUEST_URI'].'</a></td></tr>'; ?>
<?php if(!empty($_SERVER['HTTP_REFERER'])) echo '<tr><td align="right">Referer:</td><td><a href="'.$_SERVER['HTTP_REFERER'].'">'.$_SERVER['HTTP_REFERER'].'</a></td></tr>'; ?>
</table>
<?php
}#-#oops()
}//CLASS Database
###################################################################################################
?>