-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.php
More file actions
286 lines (228 loc) · 6.21 KB
/
database.php
File metadata and controls
286 lines (228 loc) · 6.21 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
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
<?php
// check if the DB has already been defined then exit
if ( defined("DB") )
return;
define ("DB","yes");
DEFINE("DB_USER_ID", "root");
DEFINE("DB_USER_PWD", "");
DEFINE("DATABASE", "project1");
DEFINE("DBHOST", "localhost");
// DEFINE("DB_USER_ID", "theproje_technyx");
// DEFINE("DB_USER_PWD", "voS$[huaRny]");
// DEFINE("DATABASE", "theproje_gm_tool");
// DEFINE("DBHOST", "localhost");
DEFINE("ER_DUP_KEY", 1022);
DEFINE("ER_DUP_ENTRY", 1062);
//-----------------------------//
// Class for Connecting Database
Class DB
{
// All the local class variables
var $con; // Holds Connection String
var $command; // Holds Sql Command
var $errorNo; // Holds Error Occored by SQLs
var $error = " ERROR "; // Holds Other Runtime Errors
var $result; // Holds Results from methods etc.
var $transactionStarted = false; // ?
var $sql; // Holds SQL Statements ?
var $debug; // Holds Debug Mode
var $showErrors; // Holds Error Mode ?
// A constructor to make connection with the database
function DB()
{
$this->GetConnection(); // Make Connection
$this->showErrors = true; // Set Error to show
}
// Returns Database Connection
function GetConnection()
{
global $HOSTNAME,$text,$SERVER_ADDR;
//checks if we're running the site on local host
$this->con = @mysql_connect(DBHOST, DB_USER_ID, DB_USER_PWD );
if (! $this->con)
{
$text = "<br><b><u>An error has occurred, in the database connection:</u></b><br>
<br><b>Detail Message: </b>". mysql_error() . "<br>";
if ( empty($HOSTNAME) && (!defined("TESTRUN")) )
{
echo $text;
ob_end_flush();
exit();
}
else
{
ob_end_clean();
if (! empty($HOSTNAME))
{
$subject = "Error in database Connection.";
$body = $text;
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: " . SERVICES_EMAIL . "\r\n";
mail (TO,$subject,$body,$headers);
}
$this->sendToDBSorryPage($text);
exit();
}
return false;
}
if (!(mysql_select_db(DATABASE)))
{
echo "Connection is not found";
exit;
}
return true;
}
// executes a given query and returns a reslult set
function ExecuteQuery($query)
{
global $debug;
$this->errorNo = 0;
$this->error = "";
if ($this->debug)
echo "<b>SQL:</b> $query<br>";
mysql_query('SET CHARACTER SET utf8'); //--> for adding different chars like 'arabic'
$resHandle = mysql_query($query, $this->con);
if ($resHandle == false)
{
$this->errorNo = mysql_errno();
$this->error = mysql_error();
if ($this->showErrors)
$this->ShowMySqlError($query);
return false;
}
$this->sql = $query;
$result = new Result($this, $resHandle);
return $result;
}
// Show MySql Errors
function ShowMySqlError($query)
{
echo "An error occurred: $query<br>";
echo mysql_errno() . " -- " . mysql_error();
}
// Primary Key Voilation Error
function HasPKViolated()
{
return ($this->errorNo == ER_DUP_KEY || $this->errorNo = ER_DUP_ENTRY);
}
// close a connection from Database
function closeConnection()
{
mysql_close($this->con);
}
// returns error
function getError()
{
return mysql_errno();
}
function sendToDBSorryPage($error)
{
session_register($error);
header("Location: ../errorpage/errors.php?err=$error");
}
function getLastInsertId()
{
$id = mysql_insert_id($this->con);
return $id;
}
// ends of DB class
}
// This class is to execute and retrive results
class Result
{
var $resHandle;
var $db;
var $errorNo;
var $error;
var $dateIndices; // an array of all date fields in this result (query)
var $dateFormat; //
function Result($db, $resHandle)
{
$this->resHandle = $resHandle;
$this->db = $db;
//$this->dateIndices = array();
}
function FetchAsArray()
{
return mysql_fetch_array($this->resHandle, MYSQL_ASSOC);
}
function FetchRows()
{
$rows = array();
while($data = $this->FetchAsArray())
array_push($rows, $data);
return $rows;
}
function FetchAsVars()
{
if(substr_count($this->db->sql,"distinct"))
$pattern = "/select +distinct(.*?)\sfrom\s/is";
else
$pattern = "/select(.*?)\sfrom\s/is";
if (preg_match($pattern, $this->db->sql, $fieldList))
{
//print_r($fieldList);
//$fields = preg_split("/,/",$fieldList[1]);
$row = $this->FetchAsArray();
// select fields list can be simple fields or functions which are followed by a , or a space
// they can also be aliased using as e.g. select date_format('%x',date) as dt from rfq
preg_match_all("/(\w+\.)?(\w+)(\(.*?\))?(\s+as\s+(\w+))?[,\s]?/is", $fieldList[1], $fields);
//print_r($fields);
//exit();
for($i=0; $i < count($fields[2]); $i++)
{
// name of the field is the alias specified
if ( !empty($fields[4][$i]) )
{
$fieldName = $fields[5][$i];
}
else
{
// in case there is no alias but a function has been used then the name of the
// field will be the name of the function prefixed with the field name
// e.g. sum(actFab) will become sumactFab
$fieldName = trim($fields[2][$i]);
if ( !empty($fields[3][$i]) )
$fieldName .= substr($fields[3][$i],1,-1);
}
if (empty($row))
$row = array();
global ${$fieldName};
${$fieldName} = $row[$i];
}
return $row;
}
// echo "returning false $this->sql";
return false;
}
function GetSelectedRows()
{
return mysql_num_rows($this->resHandle);
}
// returns no of tuples affected by Insert/update/delete
// returns no of tuples in a result
function GetAffectedRows()
{
//die($this->resHandle."<hr>");
return mysql_affected_rows($this->resHandle);
}
function FetchAsObject()
{
return mysql_fetch_object($this->resHandle);
}
// close result set
function CloseResultSet()
{
return mysql_free_result($this->resHandle);
}
function GoToRecord($no)
{
return mysql_data_seek($this->resHandle, $no);
}
function Close()
{
return mysql_free_result($this->resHandle);
}
// end of class result
}
?>