Simple PHP/MySQL Database Abstraction Layer. Returns queries as 1 or 2 dimensional array's. Features internal script debugger which emails database errors to designated admin email.
// ----------------------------------------------
// Written by Geoff A. Virgo, Janurary 2001
// Email: gvirgo@mithril.com
// Webpage: www.mithril.com
//
// Your's to use as you will, but don't blame me if your computer bursts into flames!!:) You may re-
distribute this library
// as you see fit as long as this section of the header remains intact.
// -----------------------------------------------
//
// This is a function library designed to abstract a MySQL database from the rest of your application.
//
// Features:
//
// - returns data as either 1 or 2 dimensional arrays
// - built-in script debugger which's emails database error's to the admin giving an error descrption, the
time of the error, and the name
// of the function from which the error occured
//
// You must define the hostname, username, password, database name, and admin email address either in a
settings file or within your script.
// This library assumes that these variables are static and will not be modified. As such, to create a
more simplied call, they are declared
// global with the functions which used them. The expected variable name are:
//
// $db_host => address of the MySQL server
// $db_user => connection username
// $db_password => connection password
// $db => name of the database to connect to
// $admin_email => the admin address(es)
//
// Useage:
//
// The following functions are the ones to call within your scripts, all other functions in this
library are called internally as needed.
//
// int modData(string sql_query, string function_name);
//
// - used to execute insert, update, and delete queries
// - accepts an sql query string and a function name as arguments
// - returns 0 on error and non-0 on success
//
// array dbReturnArray(string sql_query, string function_name);
//
// - used to execute queries which will return only one table row
// - accepts an sql query string and a function name as arguments
// - returns a 1 dimensional associative array
//
// array dbReturn2DArray(string sql_query, string function_name);
//
// - used to execute queries which will return more than one table row
// - accepts an sql query string and a function name as arguments
// - returns a 2 dimensional associative array
//
//
// ------------------------
// filename: settings.inc
// ------------------------
// define the hostname of the MySQL Server
$db_host = "";
// define the username for the MySQL Server Connection
$db_user = "";
// define the password for the MySQL Server Connection
$sb_password = "":
// define the database to use on the MySQL Server
$db = "";
// define the admininstrator email
$admin_email = "";
// get the current date and time - used by the
$date = date("m-d-Y");
$time = date("H:i:s");
// declare function which creates databases connection
function createDatabaseConnection($function) {
// declare global variables from settings file
global $db_host,$db_user,$db_password,$db;
// define variables created in this function
$link = "";
$select_error = "";
$connect_error = "";
// create database connection
if ($link = @mysql_connect($db_host,$db_user,$db_password) or $connect_error = dbConnectionError
($function,$date,$time)) {
// select database
@mysql_select_db($db) or $select_error = dbSelectionError($function,$date,$time);
// if db selection was successfully
if ($select_error == '') {
// return connection identifier
return $link;
}
// if db selection was unsuccessful
else {
// print the error message to the screen
echo $select_error;
}
}
// if attempt to create database connection failed
else {
// print the error message to the screen
echo $connect_error;
}
}
// create function which executes all insert, update, and delete queries on the database
function dbModData($sql,$function) {
// define variables created in this function
$result = "";
$link = ""
$sql_error = "";
// create connection to database
if ($link = createDatabaseConnection($function)) {
// execute sql query upon database
$result = @mysql_query($sql) or $sql_error = sqlError($sql,$function,$date,$time);
if ($sql_error != '') {
// result error message if query fails
echo $sql_error;
}
// close database connection
@mysql_close($link);
// free the query result identifer
@mysql_free_result();
}
// result query result
return $result;
}
// create function which executes all record retrieval queries on the database
function dbReturnArray($sql,$function) {
// define variables created in this function
$result_array = "";
$link = ""
$sql_error = "";
$process_error = "";
// create connection to database
if ($link = createDatabaseConnection($function)) {
// execute sql query upon database
$result = @mysql_query($sql) or $sql_error = sqlError($sql,$function,$date,$time);
// if query was successfully executed
if ($sql_error == '') {
// read query result into an array
$result_array = @mysql_fetch_array($result);
// if the returned query was processed properly
if ($process_error == '') {
// return the query result
return $result_array;
}
}
// if query was not successfully executed
else {
// print error message
echo $sql_error;
}
// close database connection
@mysql_close($link);
// free the query result identifer
@mysql_free_result();
}
}
// create function which executes all insert, update, and delete queries on the database
function dbReturn2DArray($sql,$function) {
// define variables created in this function
$result_array = "";
$link = ""
$sql_error = "";
$process_error = "";
// create connection to database
if ($link = createDatabaseConnection($function)) {
// execute sql query upon database
$result = @mysql_query($sql) or $sql_error = sqlError($sql,$function,$date,$time);
// if query was successfully executed
if ($sql_error == '') {
// loop query result into a 2-dimensional array
while ($myrow = @mysql_fetch_array($result)) {
// if the returned query was processed properly
if ($process_error == '') {
$result_array[] = $myrow;
}
// if the returned query was not processed properly
else {
// set the result array equal to a null value
$result_array = "";
// break out of the loop
break;
}
}
// return the query result
return $result_array;
}
// if query was not successfully executed
else {
// print the error message
echo $sql_error;
}
// close database connection
@mysql_close($link);
// free the query result identifer
@mysql_free_result();
}
}
// create function called upon failure to connect to the database
function dbConnectionError($function,$date,$time) {
// declare global variables containing current date and time
global $date,$time;
// create email body
$body = "On $date at $time, a user was unable to connect to the database server. This error occured in
$function.";
// send the error email notification
errorMailer($body);
// return an error message
return "We are currently unable to connect to the database server. The Webmaster has been
notified of this error and should have the problem resolved soon.";
}
// create function called upon failure to connect to select the correct database
function dbSelectionError($function,$date,$time) {
// declare global variables containing current date and time
global $date,$time;
// create email body
$body = "On $date at $time, a user connected to the database server but was unable to select the
correct database. ";
$body .= "This error occured in $function.";
// send the error email notification
errorMailer($body);
// return an error message
return "We are currently unable to select the correct database. Mithril.com's Webmaster has been
notified of this error and should have the problem resolved soon.";
}
// create function called upon sql query execution errors
function sqlError($sql,$function,$date,$time) {
// declare global variables containing current date and time
global $date,$time;
// create email body
$body = "On $date at $time, an sql error occured in $function. The offending sql statement is below:";
$body .= "\n\n\n$sql";
// send the error email notification
errorMailer($body);
// return an error message
return "We are currently unable to process your request. The Webmaster has been notified of this
error and should have the problem resolved soon.";
}
// declare the function which sends the debugging emails created by the functions in this file
function errorMailer($body) {
// create global variables from settings file
global $admin_email,$SERVER_NAME,$SERVER_SOFTWARE;
// send email to indentified administrator(s)
@mail("$admin_email", "Script Error", "$body", "From: \"Automated Script Debugger System at
$SERVER_NAME\"\nReply-To: \"\"\nX-Mailer: $SERVER_NAME via $SERVER_SOFTWARE");
}