WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : 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.
Categories : PHP, MySQL, Databases Click here to Update Your Picture
Geoff Virgo
Date : Feb 18th 2001
Grade : 2 of 5 (graded 1 times)
Viewed : 8530
File : lib_mysql_abstraction.zip
Images : No Images for this code example.
Search : More code by Geoff Virgo
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

<?php
        
        // ----------------------------------------------
        // 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 = "";


        // -----------------------------------------
        // filename: lib_mysql_abstraction.php
        // -----------------------------------------

         // 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.&nbsp; 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.&nbsp; 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.&nbsp; 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");
        }

?>



bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
Simple db results paging example
Categories : PHP, MySQL, Databases, Form Processing
A Complete table(ADD,EDIT,VIEW,DELETE) management System PHP,MYSQL, JAVASCRIPT
Categories : PHP, MySQL, Java Script, Databases
Convert a File database into MySQL
Categories : PHP, Filesystem, Databases, MySQL, Beginner Guides
for each record, do this to the first record, and do that to any subsequent record
Categories : PHP, Databases, MySQL, Beginner Guides
DBXML- A Class to backup databases in XML Format using web interface
Categories : PHP, PHP Classes, Databases, MySQL, XML
Dynamically generated pop-ups (Select items)
Categories : PHP, HTML and PHP, MySQL, Databases
DB Connection Function with error handling and email failure notices
Categories : PHP, MySQL, Errors and Logging, Databases, Errors and Logging
database,php,mysql,demo,example,php3,training,tutorial,codes,code
Categories : Databases, MySQL, PHP, PHP Options and Info
Displaying records of database in more than one page (paging)
Categories : Databases, MySQL, PHP
MySQL Class to ease Database connectivity
Categories : MySQL, PHP Classes, Databases, PHP
This is a very simple BBS that uses MySQL
Categories : MySQL, Databases, Complete Programs, PHP
Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs.
Categories : Databases, PHP, MySQL, Complete Programs
Create and restore backup of MySQL databases
Categories : MySQL, Databases, PHP, PHP Classes, Complete Programs