database class php

Updated the database class

Usage:

1
2
3
4
5
6
 
// min instantiation
$dataBase = new DB('hostname','username','password','database');
 
// max instantiation
$dataBase = new DB('hostname','username','password','database',DEBUG_LEVEL,array('admin@domain.com','admin@domain2.com));

Class:

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
 
class DB{
 
	/* 
	 * DESCRIPTION:		Object class, Handles all database requests and returns content accordingly
	 * WRITER:			Adrian Callaghan 12,08,08      
	 * UPDATED: 		Adrian Callaghan 08,08,11
	 * 
	 * 
	 * errLvl 
	 * 		(0), none, 
	 * 		(1) show a user error, 
	 * 		(2) (1) + mysql error
	 * 		(3) (2) + show a full error (mysql errors), 
	 * 		(4) (3) + plus debugging 
	*/
 
 
	private $host, $usr, $pwd, $errLvl, $mySqlHost, $dataBase, $emailAddresses;
 
 
	public function __construct($host, $usr, $pwd, $db, $errLvl=0, $adminEmails=array()) {
 
		/*
		 * Sets up a new connection
		 */
 
		// MIN required
		$this->setHost($host);
		$this->setUsr($usr);
		$this->setPwd($pwd);
 
		$this->setErrLvl($errLvl); // sets defualt RTE
		$this->setEmailErrsTo($adminEmails);
 
		// connect
		$this->connect();
		$this->setDb($db);
 
		}
 	public function __destruct(){
 		/*
 		 * Close the connection
 		 */
 		$this->disconnect();
 	}
	protected function setHost($host='localhost'){
		/*
		 * Sets the hostname
		 */
		$this->host = $host;
	}	
 	protected function setUsr($usr=''){
		/*
		 * Sets the username
		 */
 		$this->usr = $usr;
	}	
	protected function setPwd($pwd=''){
		/*
		 * Sets the password
		 */
		$this->pwd = $pwd;
	}	
	protected function setErrLvl($errLvl=0){
		/*
		 * Sets the error reporting level
		 */
		$this->errLvl = $errLvl;
	}	
	protected function connect(){
		/*
		 * Connect to server
		 */
		$this->mySqlHost = @mysql_connect($this->host, $this->usr, $this->pwd);
		if (!$this->mySqlHost) {
			$this->error('Cannot connect to server',mysql_error());
		}	
 
	}
	protected function disconnect() {
		/*
		 * closes current connection
		 */ 
		@mysql_close($this->mySqlHost);
	}
	public function setDb($db){
		/*
		 * Set the database
		 */
		$this->dataBase = @mysql_select_db($db,$this->mySqlHost);
		if (!$this->dataBase) {
			$this->error('Cannot select database',mysql_error());
		}
	}
	public function setEmailErrsTo($email = array()){
 
		// ensure it is an array
		$email = !is_array($email) ? array($email) : $email;
 
		// if empty return
		if (empty($email)){
			return;
		}
 
		// assign the email addresses
		$this->emailAddresses = $email;
	}
 
 
 
 
	function getQuery($SQL){
		// executes a query
		$result = @mysql_query($SQL);
		if (!$result) {
			$this->error('Invalid query',mysql_error(),$SQL);
			}
		// create a 2D array of results
		$return = array();
		while ($row = mysql_fetch_assoc($result)) $return[] = $row;
		return $return;
		}
 
 
 
 
	function setQuery($SQL){
		// executes a query without returning any results, used for insert, create etc, returns the ID of the last auto-increment
		$result = @mysql_query($SQL);
		if (!$result) {
			$this->error('Invalid query',mysql_error(),$SQL);
			}
		$return = mysql_insert_id();
		return $return;
		}
 
 
 
 
	public function error($message, $mysqlErr='', $SQL=''){
 
		/*
		 * Handles errors
		 * 
		 */
 
		switch ($this->errLvl){
 
			case 0: break;
			case 1:	$mysqlErr=''; // kill the mysql output
			case 2:	$SQL=''; // kill the sql output
			case 3: 
			case 4: 
				echo "<h1 style='color:#444444;'>$message</h1><hr>";
				if ($mysqlErr!=='') {
					echo "<p><span style='color:#ff0000; font-weight:bold;'>Mysql: </span>$mysqlErr</p>";
				}
				if ($SQL!=='') {
					echo "<p><span style='color:#ff0000; font-weight:bold;'>Whole query: </span>$SQL</p>";
				}			
 
		}
 
 		$email = $this->generateEmail(array('Error'=>$message,'Mysql'=>$mysqlErr,'Sql'=>$SQL,'Err Level'=>$this->errLvl));
 
 		// three is for debugging
 		if ($this->errLvl>3){
			echo $email;
		}
 
		if (!empty($this->emailAddresses)){
 
			$sent = $this->sendEmails($email);
 
 			if ($sent) echo '<h2>The administrator has been notified</h2>';
			else {
				echo '<h2>Please notify the administrator';
				echo count($this->emailAddresses)>1 ? "'s " : " ";
				echo implode(' or ',$this->emailAddresses);
				echo '</h2>';
			}
 
		}
 
		exit();
	}	
	private function generateEmail($err=array()){
 
		$fields = array(
			'Server name'=>$_SERVER['SERVER_NAME'],
			'Query'=>$_SERVER['SCRIPT_FILENAME'].'?'.$_SERVER['QUERY_STRING'],
			'Ip'=>$_SERVER['REMOTE_ADDR'].':'.$_SERVER['REMOTE_PORT'],
			'@'=>date("D dS M,Y h:i a")
		);	
 
		$rtn = '<table bgcolor="#cccccc">';
		foreach (array_merge($fields, $err) AS $title=>$value){
			$rtn.="<tr><td bgcolor='#aaaaaa'>$title</td><td bgcolor='#bbbbbb'>$value</td></tr>";
		}
		return $rtn.'</table>';
 
	}
	public function sendEmails($reciptents=array()){
 
		// email message
		$Name = $_SERVER['SERVER_NAME']; //senders name 
		$email = "noreply@".$_SERVER['SERVER_NAME']; //senders e-mail adress 
 
		$subject = $_SERVER['SERVER_NAME']." Error"; //subject 
 
		$header = 'MIME-Version: 1.0' . "\r\n";
		$header.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";		
		$header.= "From: ". $Name . " <" . $email . ">\r\n"; 
 
		$success=false;
		foreach ($this->emailAddresses AS $admin_email){
			$sent = @mail($admin_email, $subject, $mail_body, $header);
			if ($sent) $success=true; // holds state for any email being succesfully sent
		}
		return $success;
	}
}
VN:F [1.9.9_1125]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.9_1125]
Rating: 0 (from 0 votes)

5,999 views

This entry was posted on Monday, August 8th, 2011 at 6:05 pm and is filed under Php, Programming. You can follow any responses to this entry through the RSS 2.0 feed.

Leave a Reply