Database Code

  Home arrow Database Code arrow DB Interaction Classes v1.1
DATABASE CODE

DB Interaction Classes v1.1
By: lig
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2005-12-30

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    A remake of my original DB Interaction classes. Now there is only 2 classes (DBConnect and DBResult) and more functionality. Again DBConnect handles the connection and interaction to the database. DBResult handles the database result set. PHPDoc code added.DBConnect.class.php:
    <?php // vim: expandtab sw=4 ts=4 fdm=marker
    /**
    * DBConnect
    *
    * a simple DB connection class for MySQL that I am writing completely only on my own.
    * Well maybe not completely. I have been looking at various classes written
    * by others (the phpc class by Ben Ramsey and another class provided by a
    * tutorial) for inspiration. This class should hopefully allow for the
    * connection and selection of a MySQL DB, as well as a way to query it and get
    * back an DBResult object.
    *
    * @var $link
    * @var $result
    * @uses DBResult
    * @package
    * @version .9
    * @copyright 2006 Ligaya Turmelle
    * @author Ligaya Turmelle <lig@maolek.com>
    * @license Creative Commons Attribution-NonCommercial 2.5 License {@link http://creativecommons.org/licenses/by-nc/2.5/}
    */
    class DBConnect
    {
    /* {{{attributes */
    /**
    * link
    *
    * Link to the database
    *
    * @var mixed
    * @access public
    */
    var $link;

    /**
    * result
    *
    * Result resource
    *
    * @var mixed
    * @access public
    */
    var $result;

    /* }}}attributes */

    /* {{{constructor */
    /**
    * DBConnect
    *
    * @access public
    * @return void
    */
    function DBConnect()
    {
    // I'm like a man - taking up space and pretending to be important ;)
    }
    /* }}}constructor

    /* {{{connect */
    /**
    * connect
    *
    * Connect to the database persistantly or not. Then select your database.
    * Returns True or False for a connection.
    *
    * @param string $dbuser
    * @param string $dbpass
    * @param string $db
    * @param string $dbhost
    * @param mixed $persist
    * @access public
    * @return boolean
    */
    function connect($dbuser='', $dbpass='', $db='test', $dbhost='localhost', $persist = false)
    {
    if(!$persist)
    {
    $this->link = mysql_connect($dbhost, $dbuser, $dbpass);
    }
    else
    {
    $this->link = mysql_connect($dbhost, $dbuser, $dbpass);
    }

    if($this->link == FALSE)
    {
    return FALSE;
    }
    return(mysql_select_db($db, $this->link));
    }
    /* }}}connect */

    /* {{{errorMessage */
    /**
    * errorMessage
    *
    * Find out what the heck went wrong
    *
    * @access public
    * @return string
    */
    function errorMessage()
    {
    return mysql_error($this->link);
    }
    /* }}}errorMessage */

    /* {{{query */
    /**
    * query
    *
    * Send the query to the database. Returns either a DBResult or False
    *
    * @uses DBResult
    * @param string $sql
    * @access public
    * @return DBResult object or False
    */
    function &query($sql='')
    {
    $result = mysql_query($sql, $this->link);
    if($result != FALSE)
    {
    $this->result =& new DBResult(&$result);
    return $this->result;
    }
    else
    {
    return $result;
    }
    }
    /* }}}query */

    /* {{{affectedRows */
    /**
    * affectedRows
    *
    * Number of rows affected by the query.
    *
    * @access public
    * @return integer is returned or -1 on failure
    */
    function affectedRows()
    {
    return mysql_affected_rows($this->link);
    }
    /* }}}affectedRows */

    /* {{{lastInsertId */
    /**
    * lastInsertId
    *
    * The ID generated for an AUTO_INCREMENT column by the previous
    * INSERT query. Returns the ID or 0 if the previous
    * query does not generate an AUTO_INCREMENT value, or FALSE if
    * no MySQL connection was established.
    *
    * @access public
    * @return integer or FALSE
    */
    function lastInsertId()
    {
    return mysql_insert_id($this->link);
    }
    /* }}}lastInsertId */

    /* {{{escapeString */
    /**
    * escapeString
    *
    * This will escape a string for insertion into the MySQL database.
    *
    * @param string
    * @access public
    * @return string or FALSE
    */
    function escapeString($string)
    {
    return mysql_real_escape_string($string);
    }
    /* }}}escapeString */

    /* {{{close */
    /**
    * close
    *
    * @access public
    * @return boolean
    */
    function close()
    {
    return mysql_close($this->link);
    }
    /* }}}close */

    }
    ?>

    ********************************************
    DBResult.class.php:
    <?php // vim: expandtab sw=4 ts=4 fdm=marker
    /**
    * DBResult
    *
    * provides a means to get the database results as well as other misc. information
    * This class provides a means for the user to fetch the database results by various
    * methods. It will also allow the user to retreive various misc. information
    * pertaining to that result set.
    *
    * @var result
    * @package
    * @version .7
    * @copyright 2006 Ligaya Turmelle
    * @author Ligaya Turmelle <lig@maolek.com>
    * @license Creative Commons Attribution-NonCommercial 2.5 License {@link http://creativecommons.org/licenses/by-nc/2.5/}
    */
    class DBResult
    {
    /* {{{attributes */
    /**
    * result
    *
    * Holds the result resource from the query
    *
    * @var mixed
    * @access public
    */
    var $result;
    /* }}}attributes */

    /* {{{constructor */
    /**

    */
    /**
    * DBResult
    *
    * Get the result resource. A reference to the ressult set is expected.
    *
    * @param reference to resource
    * @access public
    * @return void
    */
    function DBResult(&$result)
    {
    $this->result = $result;
    }
    /* }}}constructor */

    /* {{{fetch */
    /**
    * fetch
    *
    * Fetch the a row of the results of the query into an array. Both the
    * associative and numerical indexes are available. A reference to the
    * array is returned or FALSE if there are no more rows.
    *
    * @access public
    * @return reference to array or FALSE
    */
    function &fetch()
    {
    return mysql_fetch_array($this->result);
    }
    /* }}}fetch */

    /* {{{fetchObject */
    /**
    * fetchObject
    *
    * Fetch the a row of the results of the query into an array. Both the
    * associative and numerical indexes are available. A reference to the
    * object is returned or FALSE if there are no more rows.
    *
    * @access public
    * @return reference to object or FALSE
    */
    function &fetchObject()
    {
    return mysql_fetch_object($this->result);
    }
    /* }}}fetchObject */

    /* {{{fetchOne */
    /**
    * fetchOne
    *
    * Fetch the data in the first cell of the first column. A reference
    * to the data is returned or FALSE on failure.
    *
    * @access public
    * @return reference to data or FALSE
    */
    function &fetchOne()
    {
    return mysql_result($this->result, 0);
    }
    /* }}}fetchOne */

    /* {{{numRows */
    /**
    * numRows
    *
    * Number of rows in the result set. An integer is returned or
    * FALSE on failure.
    *
    * @access public
    * @return integer or FALSE
    */
    function numRows()
    {
    return mysql_num_rows($this->result);
    }
    /* }}}numRows */

    /* {{{insertId */
    /**
    * insertId
    *
    * Return the ID of the last insert into an auto_increment field.
    *
    * @access public
    * @return integer
    */
    function insertId()
    {
    return mysql_insert_id();
    }
    /* }}}insertId */
    }
    ?>

    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Database Code Articles
    More By lig

    blog comments powered by Disqus

    DATABASE CODE ARTICLES

    - Converting CSV Files to MySQL Insert Queries...
    - Examples and Tools for Database Design
    - Relationships, Entities and Database Design
    - Modeling and Designing Databases
    - Data extract to Excel
    - Oracle database class 0.76
    - The opposite of mysql_fetch_assoc
    - On line Thermal Transmitance Calculation
    - pjjTextBase
    - PHP Object Generator
    - FastMySQL
    - RC4PHP
    - SQL function with integrated sprintf()
    - DB Interaction Classes v1.1
    - deeMySQLParser

    Developer Shed Affiliates

     



    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap