2009-01-07 22:51:12 - Article

Database class for php

Content.exe

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

// 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

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){
			?>
			


:
Whole query:



'.$_SERVER['SERVER_NAME'].' error'. $err.'
'. '

Request details

'. 'Script filename and query string: '.$_SERVER['SCRIPT_FILENAME'].'?'.$_SERVER['QUERY_STRING'].'
'. 'Server name: '.$_SERVER['SERVER_NAME'].'
'. '

User details

'. 'IP ADDRESS: '.$_SERVER['REMOTE_ADDR'].' : '.$_SERVER['REMOTE_PORT'].'
'. 'Time: '.date("D dS M,Y h:i a").'
' ; $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.='

The administrator has been notified

'; else $display.='

Please notify the administrator '.admin_email.'

'; $display.='

Please call back again later

'; die($display); } } ?>
Comments.log - 1 entry

One response to “Database class for php

  1. Pingback: Table pagination example PHP - Adrian Callaghan`s Blog

Leave a Response

System Notice: Your email address will not be published. Required fields are marked with *

LOADING...
Initializing CyberDev systems...