A basic PHP MySQL Handler Class

Download

Used Like

<?php

    
require_once("MysqlHandler.php");
    
$myMysql = new MysqlHandler();

    
//CHANGE THIS PASSWORD TO SOMETHING BETTER!!!!!

    
try {
        
$conn $myMysql->connect("localhost","username","password");
        
$myMysql->selectDB("database");
        
$queryResource $myMysql->query("your query here");
        
$results $myMysql->fetchArray($queryResource);
        
var_dump($results);

    } catch (
Exception $e) {
        echo 
'Exception caught at: '$e->getMessage();
    }
?>

MysqlHandler Class

<?php

    
/*!*****************************************************************************
    * 
    *  \file:               includes/classes/MysqlHandler.php
    *  \brief               Handles connecting to and retrieving information from
    *                       the database.
    *  \author:             Chris Jones
    *  \date:               10/01/06
    *  \version:            1.0001 - initial version
    *  \version:            1.0002 - Added constructor function and error function
    *  \version:            1.0003 - Added mysqlNumRows function
    *  \version:            1.0004 - Got the error emails working
    * 
    *******************************************************************************/

    /*!*****************************************************************************
    *
    *  \class:              MysqlHandler
    *  \brief:              Class to handle mysql connections
    *  \author:             Chris Jones
    *  \date:               10/01/06
    *  \version:            1.0001 - initial version
    *
    *******************************************************************************/
    
class MysqlHandler
    
{
        
//!MySQL link identifier
        
protected $conn;
        
//!Mailing list variable for errors
        
protected $mailList;

        
/*!****************************************************************************
        *
        *   \fn:                function MysqlHandler::MysqlHandler()
        *   \brief:             Constructor.  Is only used to initialize variables
        *
        *   Detailed Description:
        *   usage:              $myMysql = new MysqlHandler();
        *   \return:            Nothing
        *   \author:            Chris Jones
        *   \date:              10/01/06
        *   \version:           1.0001 - initial version (CJones)
        * 
        ******************************************************************************/
        
function MysqlHandler()
        {
            
$this->mailList .= "your@email.com";
        }

        
/*!****************************************************************************
        *
        *   \fn:                function MysqlHandler::connect($host, $username, $password)
        *   \brief:             Connects to a mysql database
        *
        *   Detailed Description:
        *   usage:              $myMysql = new MysqlHandler();
        *                       $conn = myMysql->connect($host, $username, $password);
        *
        *   \param              $host - the server the database is on
        *   \param              $username - the username to connect with
        *   \param              $password - the password to connect with
        *   \return:            A mysql link identifier
        *   \author:            Chris Jones
        *   \date:              10/01/06
        *   \version:           1.0001 - initial version (CJones)
        * 
        ******************************************************************************/
        
public function connect($host$username$password)
        {
            
$this->conn mysql_connect($host$username$password);

            if(!
$this->conn)
            {
                
$this->error(mysql_error());
                return 
false;
            }
            else{
                return 
$this->conn;
            }
        }

        
/*!****************************************************************************
        *
        *   \fn:                function MysqlHandler::selectDB($inDB)
        *   \brief:             Selects the database to use
        *
        *   Detailed Description:
        *   usage:              $myMysql = new MysqlHandler();
        *                       $conn = myMysql->connect($host, $username, $password);
        *                       $dbSelected = myMysql->selectDB($conn);
        *
        *   \param              $inDB - mysql link identifier
        *   \return:            TRUE on success or FALSE on failure
        *   \author:            Chris Jones
        *   \date:              10/01/06
        *   \version:           1.0001 - initial version (CJones)
        * 
        ******************************************************************************/
        
public function selectDB($inDB)
        {
            return 
mysql_select_db($inDB);
        }

        
/*!****************************************************************************
        *
        *   \fn:                function MysqlHandler::query($inQuery)
        *   \brief:             Runs a query the database.
        *
        *   Detailed Description:
        *   usage:              $myMysql = new MysqlHandler();
        *                       $conn = myMysql->mconnect($host, $username, $password);
        *                       $query = myMysql->query("your query here");
        *
        *   \param              $inQuery - mysql query string
        *   \return:            For SELECT, SHOW, DESCRIBE or EXPLAIN statements, 
        *                       returns a resource on success, or FALSE on error.
        *                       For other type of SQL statements, UPDATE, DELETE, DROP, etc,
        *                       mysql_query() returns TRUE on success or FALSE on error. 
        *   \author:            Chris Jones
        *   \date:              10/01/06
        *   \version:           1.0001 - initial version (CJones)
        * 
        ******************************************************************************/
        
public function query($inQuery)
        {
            
$query mysql_query($inQuery);

            if(!
$query)
            {
                
$errMsg "$inQuery\n\n";
                
$errMsg .= mysql_error();
                
$this->error($errMsg);
                return 
false;
            }
            else
            {
                return 
$query;
            }
        }

        
/*!****************************************************************************
        *
        *   \fn:                function MysqlHandler::fetchArray($inResource)
        *   \brief:             Fetches the result of a query as an associative array
        *
        *   Detailed Description:
        *   usage:              $myMysql = new MysqlHandler();
        *                       $conn = myMysql->connect($host, $username, $password);
        *                       $query = myMysql->query("your query here");
        *                       $result = myMysql->fetchArray($query);
        *
        *   \param              $inResource - mysql resource
        *   \return:            Returns an array that corresponds to the fetched row, or
        *                       FALSE  if there are no more rows.  success or FALSE on error. 
        *   \author:            Chris Jones
        *   \date:              10/01/06
        *   \version:           1.0001 - initial version (CJones)
        * 
        ******************************************************************************/
        
public function fetchArray($inResource)
        {
            return 
mysql_fetch_array($inResourceMYSQL_ASSOC);
        }

        
/*!****************************************************************************
        *
        *   \fn:                function MysqlHandler::fetchRow($inResource)
        *   \brief:             Fetches the result of a query as an enumerated array
        *
        *   Detailed Description:
        *   usage:              $myMysql = new MysqlHandler();
        *                       $conn = myMysql->connect($host, $username, $password);
        *                       $query = myMysql->query("your query here");
        *                       $result = myMysql->fetchRow($query);
        *
        *   \param              $inResource - mysql resource
        *   \return:            Returns an array that corresponds to the fetched row, or
        *                       FALSE  if there are no more rows.  success or FALSE on error. 
        *   \author:            Chris Jones
        *   \date:              10/01/06
        *   \version:           1.0001 - initial version (CJones)
        * 
        ******************************************************************************/
        
public function fetchRow($inResource)
        {
            return 
mysql_fetch_row($inResource);
        }

        
/*!****************************************************************************
        *
        *   \fn:                function MysqlHandler::numRows($inResource)
        *   \brief:             Fetches the number of rows a query returns
        *
        *   Detailed Description:
        *   usage:              $myMysql = new MysqlHandler();
        *                       $conn = myMysql->connect($host, $username, $password);
        *                       $query = myMysql->query("your query here");
        *                       $numRows = myMysql->numRows($query);
        *
        *   \param              $inResource - mysql resource
        *   \return:            Returns the number of rows a query returns 
        *   \author:            Chris Jones
        *   \date:              10/01/06
        *   \version:           1.0001 - initial version (CJones)
        * 
        ******************************************************************************/
        
public function numRows($inResource)
        {
            return 
mysql_num_rows($inResource);
        }

        
/*!****************************************************************************
        *
        *   \fn:                function MysqlHandler::error()
        *   \brief:             Error handler.  It prints a message to the user that
        *                       an error has occured and emails a list of people with
        *                       the details of the error.
        *
        *   Detailed Description:
        *   usage:              Used internaly.
        *                       $this->error(mysql_error());
        *   \return:            Nothing
        *   \author:            Chris Jones
        *   \date:              10/01/06
        *   \version:           1.0001 - initial version (CJones)
        * 
        ******************************************************************************/
        
protected function error($inErr)
        {
            throw new 
Exception("A database error has occurred. $inErr");
        }
    }

?>