Database Code
  Home arrow Database Code arrow DB Interaction Classes v1.1
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
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:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Calling all CC Power Users – and those that would like to be!

    Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development
    FREE! Go There Now!


    NEW! Cook up Web sites fast with CakePHP, Part 4: Use CakePHP&apos;s Session and Request Handler components

    CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP.
    FREE! Go There Now!


    NEW! Download the free Web Application Security eKit

    Discover how IBM Rational AppScan Standard Edition can help you detext vulnerabilities in your web applications in the Web Application Security eKit. IBM Rational AppScan is a leading suite of automated web application security solutions that scan and test for common Web application vulnerabilities. The new Web Application Security eKit provides you with valuable resources, including white papers, demos, and additional information on the benefits of testing your Web applications.
    FREE! Go There Now!


    NEW! Innovate don't duplicate! Asset reuse strategies for success

    Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository.
    FREE! Go There Now!


    NEW! Integrating XML into Your Enterprise Using Data Federation

    XML has become a common way of storing business data as flat files and many data server vendors including IBM have provided ways to store this data within relational database systems. Increasingly collections of XML files are accessed like databases using an xQuery and other XML standard mechanisms. Businesses find the need to combine the traditional tabular structured data with XML formatted data. In this webcast, you’ll learn about IBM’s WebSphere Federation Server technology, which provides users with the ability to integrate these two data formats.
    FREE! Go There Now!


    NEW! Rational Modeling Extension for Microsoft.Net

    Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms.
    FREE! Go There Now!


    NEW! Rational Talks to You:Per Kroll on Rational Method Composer Plug-in customization

    Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered!
    FREE! Go There Now!


    NEW! Rational Talks to You: Manage RUP-based CMMI initiatives

    Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered!
    FREE! Go There Now!


    NEW! Try the IBM SOA Sandbox for Process

    Visit IBM developerWorks to try the IBM SOA Sandbox for process. The SOA Sandbox for process focuses on providing a trial environment with the necessary tooling and components required to gain a better understanding of business processes and how to best improve existing business processes to derive value quickly.
    FREE! Go There Now!


    NEW! Using Rational Business Developer to enhance your developer productivity

    Join this Rational Talks to You teleconference, to hear how Enterprise Generation Language (EGL) eliminates the need for tedious and error-prone low level coding, so developers can focus on business requirements. EGL extends the Rational software development platform with a simplified programming language that enables developers who have little or no experience with Java, Web technologies or Service Oriented Architecture, to create enterprise-class applications and services quickly and easily. It also allows developers who may have little or no mainframe programming experience to quickly create traditional mainframe components.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    DATABASE CODE ARTICLES

    - 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
    - CSV to SQL convertor






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway