| 1 |
paul |
1 |
<?php |
|
|
2 |
/****************************************************************************** |
|
|
3 |
* Database Abstraction Class |
|
|
4 |
* DB Type: MySQL |
|
|
5 |
* (C) Paul Clement 2007 |
|
|
6 |
* |
|
|
7 |
* Usage: $DB = new DataBase('Host', 'User', 'Password', 'DB'); |
|
|
8 |
******************************************************************************/ |
|
|
9 |
|
|
|
10 |
class DataBase |
|
|
11 |
{ |
|
|
12 |
// Connection Variable |
|
|
13 |
public $Connection; |
|
|
14 |
|
|
|
15 |
// Initialise Connection |
|
|
16 |
function __construct($DBHost, $DBUser, $DBPass, $DBName) |
|
|
17 |
{ |
|
|
18 |
// Attempt Connection |
|
|
19 |
$this->Connection = @mysql_connect($DBHost, $DBUser, $DBPass); |
|
|
20 |
|
|
|
21 |
// Verify Connection |
|
|
22 |
if ( !$this->Connection ) |
|
|
23 |
{ |
|
|
24 |
// Throw Error |
|
|
25 |
trigger_error("MySQL Error " . mysql_errno() . ": " . mysql_error(), E_USER_WARNING); |
|
|
26 |
return false; |
|
|
27 |
exit(); |
|
|
28 |
} |
|
|
29 |
|
|
|
30 |
// Select Database |
|
|
31 |
$Select = @mysql_select_db($DBName, $this->Connection); |
|
|
32 |
|
|
|
33 |
// Verify Selection |
|
|
34 |
if ( !$Select ) |
|
|
35 |
{ |
|
|
36 |
// Throw Error |
|
|
37 |
trigger_error("MySQL Error " . mysql_errno($this->Connection) . ": " . mysql_error($this->CONNECTION_ABORTED), E_USER_WARNING); |
|
|
38 |
return false; |
|
|
39 |
exit(); |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
// Return true |
|
|
43 |
return true; |
|
|
44 |
} |
|
|
45 |
|
|
|
46 |
// Execute SQL Command |
|
|
47 |
function ExecuteSQL($SQLCommand) |
|
|
48 |
{ |
|
|
49 |
// Query Database |
|
|
50 |
$Query = mysql_query($SQLCommand, $this->Connection); |
|
|
51 |
|
|
|
52 |
// Verify Query |
|
|
53 |
if ( mysql_error($this->Connection) ) |
|
|
54 |
{ |
|
|
55 |
// Throw Error |
|
|
56 |
trigger_error("MySQL Error " . mysql_errno($this->Connection) . ": " . mysql_error($this->Connection), E_USER_WARNING); |
|
|
57 |
return false; |
|
|
58 |
exit(); |
|
|
59 |
} |
|
|
60 |
|
|
|
61 |
// Return true |
|
|
62 |
return $Query; |
|
|
63 |
} |
|
|
64 |
|
|
|
65 |
// Get Data Set |
|
|
66 |
function GetDataSet($SQLCommand) |
|
|
67 |
{ |
|
|
68 |
// Create Return Variable |
|
|
69 |
$DataSet = array(); |
|
|
70 |
|
|
|
71 |
// Query Database |
|
|
72 |
$Query = $this->ExecuteSQL($SQLCommand); |
|
|
73 |
|
|
|
74 |
// Add returned rows to array |
|
|
75 |
while ( $DataRow = mysql_fetch_array($Query, MYSQL_ASSOC) ) |
|
|
76 |
{ |
|
|
77 |
$DataSet[] = $DataRow; |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
// Return DataSet |
|
|
81 |
return $DataSet; |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
// Get Data Row |
|
|
85 |
function GetDataRow($SQLCommand) |
|
|
86 |
{ |
|
|
87 |
// Get Data Set |
|
|
88 |
$DataSet = $this->GetDataSet($SQLCommand); |
|
|
89 |
|
|
|
90 |
// Check that a row exists |
|
|
91 |
if ( count ( $DataSet ) == 0 ) |
|
|
92 |
{ |
|
|
93 |
trigger_error("GetDataRow: No rows returned!", E_USER_NOTICE); |
|
|
94 |
return false; |
|
|
95 |
exit(); |
|
|
96 |
} |
|
|
97 |
|
|
|
98 |
// Return first row from dataset |
|
|
99 |
return $DataSet[0]; |
|
|
100 |
} |
|
|
101 |
|
|
|
102 |
// Insert Row |
|
|
103 |
function InsertRow($TableName, $ValuesArray) |
|
|
104 |
{ |
|
|
105 |
// Initialise Variables |
|
|
106 |
$i = 1; |
|
|
107 |
$Columns = ""; |
|
|
108 |
$Values = ""; |
|
|
109 |
|
|
|
110 |
// Count Items |
|
|
111 |
$numItems = count($ValuesArray); |
|
|
112 |
|
|
|
113 |
// Loop through $ValuesArray |
|
|
114 |
foreach ( $ValuesArray As $ColumnName => $Value ) |
|
|
115 |
{ |
|
|
116 |
$Values .= "'" . addslashes($Value) . "'"; |
|
|
117 |
$Columns .= $ColumnName; |
|
|
118 |
|
|
|
119 |
// If not last entry |
|
|
120 |
if ( $i != $numCols ) |
|
|
121 |
{ |
|
|
122 |
// Add Seperators |
|
|
123 |
$Values .= ", "; |
|
|
124 |
$Columns .= ", "; |
|
|
125 |
} |
|
|
126 |
|
|
|
127 |
// Increment i |
|
|
128 |
$i++; |
|
|
129 |
} |
|
|
130 |
|
|
|
131 |
// Build SQL Statement |
|
|
132 |
$SQL = "INSERT INTO " . $TableName ." ( " . $Columns . " ) VALUES ( " . $Values . " )"; |
|
|
133 |
|
|
|
134 |
// Execute Query |
|
|
135 |
return $this->ExecuteSQL($SQL); |
|
|
136 |
} |
|
|
137 |
|
|
|
138 |
// Delete Row(s) |
|
|
139 |
function DeleteRow($TableName, $WhereArray) |
|
|
140 |
{ |
|
|
141 |
// Set up variables |
|
|
142 |
$i = 1; |
|
|
143 |
$Values = ""; |
|
|
144 |
$Columns = ""; |
|
|
145 |
|
|
|
146 |
// Count number of items |
|
|
147 |
$numCols = count($WhereArray); |
|
|
148 |
|
|
|
149 |
// Start SQL Statement |
|
|
150 |
$SQL = "DELETE FROM " . $TableName . " WHERE "; |
|
|
151 |
|
|
|
152 |
// Loop through $whereArray |
|
|
153 |
foreach ( $WhereArray As $ColumnName => $Value ) |
|
|
154 |
{ |
|
|
155 |
$SQL .= $ColumnName . " = '" . addslashes($Value) . "'"; |
|
|
156 |
|
|
|
157 |
// If not last entry |
|
|
158 |
if ( $i != $numCols ) |
|
|
159 |
{ |
|
|
160 |
// Add Seperators |
|
|
161 |
$SQL .= " AND "; |
|
|
162 |
} |
|
|
163 |
|
|
|
164 |
// Increment i |
|
|
165 |
$i++; |
|
|
166 |
} |
|
|
167 |
|
|
|
168 |
// Execute SQL Command |
|
|
169 |
return $this->ExecuteSQL($SQL); |
|
|
170 |
} |
|
|
171 |
|
|
|
172 |
// Update Row |
|
|
173 |
function UpdateRow($TableName, $ValuesArray, $WhereArray) |
|
|
174 |
{ |
|
|
175 |
// Set up variables |
|
|
176 |
$i = 1; |
|
|
177 |
$UpdateValues = ""; |
|
|
178 |
$UpdateColumns = ""; |
|
|
179 |
$WhereValues = ""; |
|
|
180 |
$WhereColumns = ""; |
|
|
181 |
|
|
|
182 |
// Set counts |
|
|
183 |
$whereCount = count($WhereArray); |
|
|
184 |
$valueCount = count($ValuesArray); |
|
|
185 |
|
|
|
186 |
// Start SQL String |
|
|
187 |
$SQL = "UPDATE " . $TableName . " SET "; |
|
|
188 |
|
|
|
189 |
// Loop through $ValuesArray |
|
|
190 |
foreach ( $ValuesArray As $Column => $Value ) |
|
|
191 |
{ |
|
|
192 |
$SQL .= $Column . " = '" . addslashes($Value) . "'"; |
|
|
193 |
|
|
|
194 |
// If not last entry |
|
|
195 |
if ( $i != $valueCount ) |
|
|
196 |
{ |
|
|
197 |
// Add Seperators |
|
|
198 |
$SQL .= ", "; |
|
|
199 |
} |
|
|
200 |
|
|
|
201 |
// Increment i |
|
|
202 |
$i++; |
|
|
203 |
} |
|
|
204 |
|
|
|
205 |
if ( $whereCount != 0 ) |
|
|
206 |
{ |
|
|
207 |
$SQL .= " WHERE "; |
|
|
208 |
|
|
|
209 |
$i = 1; |
|
|
210 |
// Loop through $WhereArray |
|
|
211 |
foreach ( $WhereArray As $Column => $Value ) |
|
|
212 |
{ |
|
|
213 |
$SQL .= $Column . " = '" . $Value . "'"; |
|
|
214 |
|
|
|
215 |
// If not last entry |
|
|
216 |
if ( $i != $whereCount ) |
|
|
217 |
{ |
|
|
218 |
// Add Seperators |
|
|
219 |
$SQL .= " AND "; |
|
|
220 |
} |
|
|
221 |
|
|
|
222 |
// Increment i |
|
|
223 |
$i++; |
|
|
224 |
} |
|
|
225 |
} |
|
|
226 |
|
|
|
227 |
// Execute SQL Command |
|
|
228 |
return $this->ExecuteSQL($SQL); |
|
|
229 |
} |
|
|
230 |
} |
|
|
231 |
?> |