Rev Author Line No. Line
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                 {
2 paul 116                         if ( $Value == "NULL") {
117                                 $Values .= "NULL";
118                         }
119                         else {
120                                 $Values .= "'" . addslashes($Value) . "'";
121                         }
1 paul 122                         $Columns .= $ColumnName;
123  
124                         // If not last entry
2 paul 125                         if ( $i != $numItems )
1 paul 126                         {
127                                 // Add Seperators
128                                 $Values .= ", ";
129                                 $Columns .= ", ";
130                         }
131  
132                         // Increment i
133                         $i++;
134                 }
135  
136                 // Build SQL Statement
137                 $SQL = "INSERT INTO " . $TableName ." ( " . $Columns . " ) VALUES ( " . $Values . " )";
138  
139                 // Execute Query
140                 return $this->ExecuteSQL($SQL);
141         }
142  
143         // Delete Row(s)
144         function DeleteRow($TableName, $WhereArray)
145         {
146                 // Set up variables
147                 $i = 1;
148                 $Values = "";
149                 $Columns = "";
150  
151                 // Count number of items
152                 $numCols = count($WhereArray);
153  
154                 // Start SQL Statement
155                 $SQL = "DELETE FROM " . $TableName . " WHERE ";
156  
157                 // Loop through $whereArray
158                 foreach ( $WhereArray As $ColumnName => $Value )
159                 {
160                         $SQL .= $ColumnName . " = '" . addslashes($Value) . "'";
161  
162                         // If not last entry
163                         if ( $i != $numCols )
164                         {
165                                 // Add Seperators
166                                 $SQL .= " AND ";
167                         }
168  
169                         // Increment i
170                         $i++;
171                 }
172  
173                 // Execute SQL Command
174                 return $this->ExecuteSQL($SQL);
175         }
176  
177         // Update Row
178         function UpdateRow($TableName, $ValuesArray, $WhereArray)
179         {
180                 // Set up variables
181                 $i = 1;
182                 $UpdateValues = "";
183                 $UpdateColumns = "";
184                 $WhereValues = "";
185                 $WhereColumns = "";
186  
187                 // Set counts
188                 $whereCount = count($WhereArray);
189                 $valueCount = count($ValuesArray);
190  
191                 // Start SQL String
192                 $SQL = "UPDATE " . $TableName . " SET ";
193  
194                 // Loop through $ValuesArray
195                 foreach ( $ValuesArray As $Column => $Value )
196                 {
197                         $SQL .= $Column . " = '" . addslashes($Value) . "'";
198  
199                         // If not last entry
200                         if ( $i != $valueCount )
201                         {
202                                 // Add Seperators
203                                 $SQL .= ", ";
204                         }
205  
206                         // Increment i
207                         $i++;
208                 }
209  
210                 if ( $whereCount != 0 )
211                 {
212                         $SQL .= " WHERE ";
213  
214                         $i = 1;
215                         // Loop through $WhereArray
216                         foreach ( $WhereArray As $Column => $Value )
217                         {
218                                 $SQL .= $Column . " = '" . $Value . "'";
219  
220                                 // If not last entry
221                                 if ( $i != $whereCount )
222                                 {
223                                         // Add Seperators
224                                         $SQL .= " AND ";
225                                 }
226  
227                                 // Increment i
228                                 $i++;
229                         }
230                 }
231  
232                 // Execute SQL Command
233                 return $this->ExecuteSQL($SQL);
234         }
235 }
236 ?>