Database class for php
Database class for php
$dataBase = new DB();
(instaniates the database ready for CRUD)
$result = $dataBase->getQuery(‘SELECT * FROM FOO WHERE bar=1’);
($result contains a multidimensional associative array)
$id = $dataBase->setQuery(‘DELETE FROM FOO WHERE bar=1’);
($id contains the row id of the last update, can be left out)
To settup the class
1 2 3 4 5 6 7 8 9 10 | // database define ('HOST', 'localhost'); define ('NAME', 'NAME_OF_DATABASE'); define ('USER', 'USER_NAME'); define ('PASS', 'PASSWORD'); // Error messages define ('ERRS', false); // Meaningfull error messages on or off (mainly SQL related) define ('MESSAGE', 'The server encountered an internal error');// Default message define ('admin_email', 'someone@somewhere.com');// email errors to |
Errors can be set to display on or off (uses a generic message) it then emails full details to the administrator of the fault.
It can be downloaded from here
Code below
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 | <?php class DB{ /* DESCRIPTION: Object class, Handles all database requests and returns content accordingly WRITER: Adrian Callaghan 120808 INSTANIATION: new DB() requires HOST,NAME,USER,PASS,ERRS,MESSAGE values to be set prior (config) API: MODIFIERS: setStateClose() // sets the mySql connection to close setQuery($arg) // executes a query without returning any results, used for insert, create etc, // returns the ID of the last auto-increment setErrMessage($err) // displays and emails, the sql error setDataBase($database) // sets dataBase, leave arg blank to set back to the default ACCESSORS: getQuery($arg) // executes sql query from $arg and returns result as a '2d assoc array' getStateOpen() // returns details about the currently open connection */ var $mySqlHost; var $dataBase; function __construct() { $this->mySqlHost = @mysql_connect(HOST, USER, PASS); if (!$this->mySqlHost) { $this->setErrMessage('Access Error','Error',mysql_error(),$SQL); } $this->setDataBase(); } // Accesors function getQuery($SQL){ // executes a query $result = @mysql_query($SQL); if (!$result) { $this->setErrMessage('Sql 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 getStateOpen(){ // returns an array of details about the open connection $return['DB']['host']=$this->mySqlHost; $return['DB']['database']=$this->dataBase; } // Modifiers function setDataBase($dataBaseName=NAME){ $this->dataBase = @mysql_select_db($dataBaseName,$this->mySqlHost); if (!$this->dataBase) { $this->setErrMessage('I/O Error','Error',mysql_error(),$SQL); } } function setStateClose() { // closes current connection mysql_close($mySqlHost); } 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->setErrMessage('Sql Error','Invalid query',mysql_error(),$SQL); } $return = mysql_insert_id(); return $return; } function setErrMessage($message, $errName, $err, $SQL){ // displays and emails, the sql error if (ERRS){ ?> <h1 style='color="#444444;'><?php echo $message; ?></h1> <hr> <p> <font color="#ff0000"><b><?php echo $errName; ?>: </b></font><?php echo $err; ?> <br /> <font color="#ff0000"><b>Whole query: </b></font><?php echo $SQL; ?> </p> <hr> <?php } else { ?> <h1 style='color="#444444;"'><?php echo MESSAGE; ?></h1> <hr> <?php } // email message $Name = $_SERVER['SERVER_NAME']; //senders name $email = "noreply@".$_SERVER['SERVER_NAME']; //senders e-mail adress //mail body $mail_body = '<h2>'.$_SERVER['SERVER_NAME'].' error</h2>'. $err.'<br/>'. '<h2>Request details</h2>'. '<b>Script filename and query string: </b>'.$_SERVER['SCRIPT_FILENAME'].'?'.$_SERVER['QUERY_STRING'].'<br/>'. '<b>Server name: </b>'.$_SERVER['SERVER_NAME'].'<br/>'. '<h2>User details</h2>'. '<b>IP ADDRESS: </b>'.$_SERVER['REMOTE_ADDR'].' : '.$_SERVER['REMOTE_PORT'].'<br/>'. '<b>Time: </b>'.date("D dS M,Y h:i a").'<br/>' ; $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"; // send mail, and die $sent = @mail(admin_email, $subject, $mail_body, $header) ; if ($sent) $display.='<h2>The administrator has been notified</h2>'; else $display.='<h2>Please notify the administrator '.admin_email.'</h2>'; $display.='<p>Please call back again later</p>'; die($display); } } ?> |
19,332 views
This entry was posted on Wednesday, January 7th, 2009 at 10:51 pm and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed.
[…] is designed to be used in conjunction with the database class found here but can be used standalone with any suitable […]