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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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! 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 DB2 Express-C 9.5

    Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages.
    FREE! Go There Now!


    NEW! Evaluate Rational Business Developer V7.1

    Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities.
    FREE! Go There Now!


    NEW! IBM Rational ClearCase Innovator's Series

    Learn from the best! Find out how developers use Rational ClearCase to be more flexible, innovative and deliver higher quality code in the Rational ClearCase Power Users eKit. This complimentary eKit provides a collection of materials, like articles, whitepapers, and demos that can help you become a power user of Rational ClearCase.
    FREE! Go There Now!


    NEW! Rational Talks to You: Scott Ambler on being agile in a global development environment

    Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered!
    FREE! Go There Now!


    NEW! Rational Testing eKits

    Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing.
    FREE! Go There Now!


    NEW! The role of integrated requirements management in software delivery

    This paper is about the critical role that a discipline called integrated require­ments management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrat­ing, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Tester for SOA Quality V7.0.1

    Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services.
    FREE! Go There Now!


    NEW! Webcast: Accelerating Software Innovation with System z

    Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, where he will overview Rational’s new offerings and programs to help customers accelerate software innovation on System z. He will discuss how these solutions help organizations extend their core business processes toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time.
    FREE! Go There Now!


    NEW! Webcast: Introducing the new Information Server and Solutions community: LeverageInformation

    User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges.
    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-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT