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! Applying lean thinking to the governance of software development

    Effective governance for lean development isn’t about command and control. Instead, the focus is on enabling the right behaviors and practices through collaborative and supportive techniques. Hear from Scott Ambler on how it is far more effective to motivate people to do the right thing than it is to force them to do so. Learn how to form a lightweight, collaboration-based framework that reflects the realities of modern IT organizations.
    FREE! Go There Now!


    NEW! Download a free trial of Lotus Quickr 8.0

    Visit IBM developerWorks to download a free trial version of Lotus Quickr 8.0, which enables collaboration by transforming the way everyday business content such as documents, rich media, photos, and video can be shared. Lotus Quickr makes it faster and easier to share content of all types (not just documents) within virtual teams. It is designed to make it easier to collaborate across organizational boundaries, while continuing to work within the context of familiar desktop applications.
    FREE! Go There Now!


    NEW! Download IBM Data Studio V1.1

    Visit IBM developerWorks to download the latest trial version of IBM Data Studio V1.1 at no cost. IBM Data Studio is a comprehensive data management solution that helps you effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life cycle utilizing a consistent and integrated user interface. Unlike other client-side data management solutions that focus on only one aspect of the application lifecycle or database administration, Data Studio complements the Rational Software Delivery platform, providing unparalleled flexibility for a heterogeneous data server environment across platforms.
    FREE! Go There Now!


    NEW! Download IBM WebSphere Portal V6.1 beta code

    Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization.
    FREE! Go There Now!


    NEW! Info 2.0: Harnessing the power of Web 2.0 and Enterprise Mashups

    Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started.
    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 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! Trial download: IBM Informix Dynamic Server Express Edition V11.0

    Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Manual Tester V7.0.1

    Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually.
    FREE! Go There Now!


    NEW! Webcast: Calling All Testers! Find Application Vulnerabilities Early in the Development Process Where they are Easier to Fix and Less Risky to your Business

    In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information.
    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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek