Database Code

  Home arrow Database Code arrow DB interactions classes
DATABASE CODE

DB interactions classes
By: lig
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2005-10-31

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    3 simple classes that handle database interaction and results - written in PHP4. SystemObject loads your DB access information into class vars. DBConnect directly interacts with the DB in multiple ways (connect, query, escapeString, affectedRows, ect.). DBResult fetches the results (fetch, fetchOne, NumRows).SystemObject.php:
    <?php // vim: expandtab sw=4 ts=4 fdm=marker
    /**
    * Place to hold information to be used by the whole CMS.
    *
    * @var $db database name
    * @var $dbuser username to log into database
    * @var $dbpass password to log into database
    * @var $dbhost the address of the database (localhost, mysql.domain.com)
    *
    * The following work is licensed under a Creative Commons
    * Attribution-NonCommercial 2.5 License. For more
    * information on this license go to http://creativecommons.org/licenses/by-nc/2.5/
    */

    class SystemObject
    {
    /* {{{attributes */

    /**
    * Vars to hold the DB access info - guess could put it into an array
    */
    var $db;
    var $dbuser;
    var $dbpass;
    var $dbhost;

    /* }}}attributes */

    /* {{{constructor */
    /**
    * Constructor
    */
    function SystemObject()
    {
    /*
    * Tell me where you are keeping the DB access info so I can go and get it.
    * Maybe one day figure out how to get it from a config file. Needless to
    * say the database access information is defined in the DBInfo.php page.
    * define("DATABASE_NAME","db");
    *
    * Reason for doing it this way - I DO NOT want my database access information
    * anywhere on the web root.
    */
    require_once('path\to\DBInfo.php');
    $this->db = DATABASE_NAME;
    $this->dbuser = DATABASE_USER;
    $this->dbpass = DATABASE_PASS;
    $this->dbhost = DATABASE_HOST;
    }
    /* }}}constructor */

    }
    ?>


    DBConnect.php:
    <?php // vim: expandtab sw=4 ts=4 fdm=marker
    /**
    * a simple DB connection class 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 array result resource.
    *
    * @var $link
    * @var $query
    * @var $result
    *
    * @uses DBResult
    *
    * The following work is licensed under a Creative Commons
    * Attribution-NonCommercial 2.5 License. For more
    * information on this license go to http://creativecommons.org/licenses/by-nc/2.5/
    */

    /* This assumes that the SystemObject.class.php file is in the same directory */
    require_once 'SystemObject.class.php';

    class DBConnect extends SystemObject
    {
    /* {{{attributes */
    /**
    * Link to the database
    */
    var $link;

    /**
    * Query to be sent to the database
    */
    var $query;

    /**
    * Result resource
    */
    var $result;
    /* }}}attributes */


    /* {{{constructor */
    function DBConnect()
    {
    parent::SystemObject();
    $this->link = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass);
    if($this->link == FALSE)
    {
    return FALSE;
    }
    if(!mysql_select_db($this->db, $this->link))
    {
    return FALSE;
    }
    return TRUE;
    }
    /* }}}constructor */

    /* {{{query */
    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 */
    /**
    * Number of rows affected by the query.
    * An integer is returned or -1 on failure.
    */
    function affectedRows()
    {
    return mysql_affected_rows($this->link);
    }
    /* }}}affectedRows */

    /* {{{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.
    */
    function lastInsertId()
    {
    return mysql_insert_id($this->link);
    }
    /* }}}lastInsertId */

    /* {{{escapeString */
    /**
    * This will escape a string for insertion into the MySQL database.
    * Returns the escaped string, or FALSE on error.
    */
    function escapeString($string)
    {
    return mysql_real_escape_string($string);
    }
    /* }}}escapeString */

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

    }
    ?>


    DBResult.php:
    <?php // vim: expandtab sw=4 ts=4 fdm=marker
    /**
    * 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
    *
    * The following work is licensed under a Creative Commons
    * Attribution-NonCommercial 2.5 License. For more
    * information on this license go to http://creativecommons.org/licenses/by-nc/2.5/
    */

    class DBResult
    {
    /* {{{attributes */
    /**
    * Holds the result resource from the query
    */
    var $result;
    /* }}}attributes */

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

    /* {{{fetch */
    /**
    * Fetch a row of the results from 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.
    */
    function &fetch()
    {
    return mysql_fetch_array($this->result);
    }
    /* }}}fetch */

    /* {{{fetchObject */
    /**
    * Fetch a row of the results from the query into an object. A reference to the
    * object is returned or FALSE if there are no more rows.
    *
    */
    function &fetchObject()
    {
    return mysql_fetch_object($this->result);
    }
    /* }}}fetchObject */

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

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

    }
    ?>
    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