SunQuest
 
       Zend
  Home arrow Zend arrow Page 3 - Build Database Interfaces
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
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? 
ZEND

Build Database Interfaces
By: Zend
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2004-01-04

    Table of Contents:
  • Build Database Interfaces
  • Replacement Example
  • Lessen the Impact
  • Autonomous

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Build Database Interfaces - Lessen the Impact


    (Page 3 of 4 )


    Another advantage to using a DB interface is that changes to the structure of the database have less of an impact on the application. Changing a single DB interface is far easier than finding and modifying code and embedded SQL throughout an entire application.

    Creating a Database Interface
    There are a few general guidelines that you should follow when designing your database interface:
    • Use an object oriented approach and follow OOP principles.
    • Make the DB interface autonomous and with a single purpose.
    • Base the DB interface on a standardized design.

    Use an Object-Oriented Approach
    A class should be used to create each DB interface you require. This class should be a logical (code) representation of a single database structure. The members of the class represent each column of a row. The methods of the class should perform the DML and DQL operations on the row. The example below illustrates how DDL and DQL operations are implemented through the load(), submit() and delete() methods.


    <?php 

    class Client 
    {
     
    var $clientID       0;
     
    var $firstName      '';
     
    var $lastName       '';
     
    var $emailAddress   '';

    function Client
    () {}
    function load
    (&$db,$clientID)
    {
     $DQL 
    'SELECT clientID,firstName,lastName,emailAddress '.
      
    'FROM client '.
       
    "WHERE clientID=$clientID";

    if 
    ($row $db->GetOne())
      
    {
       $clientID 
    $row'clientID' ];
       $firstName 
    $row'firstName' ];
       $lastName 
    $row'lastName' ];
       $emailAddress 
    $row'emailAddress' ];

    return true
    ;
       
    } else {
       
    return false;
       
    }
      
    }

    function submit
    (&$db)
      
    {
       
    // clean up the data.
       $clientID = (int) $this->clientID;
       $firstName = addslashes(trim( $firstName    ));
       $lastName  = addslashes(trim( $lastName     ));
       $emailAddress = addslashes(trim( $emailAddress ));

    if ($clientID == 0)
       {
       $DML = 'INSERT INTO client (clientID,firstName,lastName, '.emailAddress) VALUES (NULL,'. "'$firstName','$lastName','$emailAddress')";

    if ($db->Execute($DML))
       {
       $this->clientID = $db->Insert_ID();
       return true;
       } else {
       return false;
       }
         } else {
         $DML = 'UPDATE client SET '.
          "firstName    = '$firstName    ,' ".
          "lastName     = '$lastName     ,' ".
          "emailAddress = '$emailAddress ,' ".
          "WHERE clientID= $clientID";

    if ($db->Execute($db))
          {
           return true;
          } else {
           return false;
          }
        }
       }

    function delete(&$db)
      {
       $DML = 'DELETE FROM client WHERE clientID='.$this->clientID;
       if ($db->Execute($db))
        {
         return true;
          } else {
         return false;
          }
        }

    function getClientID()
      {
      return $this->clientID;
      }

    function setClientID($clientID)
      {
      $this->clientID = $clientID;
      }
        
    function getFirstName() 
      { 
      return $this->firstName; 
      }

    function setFirstName($firstName) 
      { 
      $this->firstName = $firstName; 
      }

    function getLastName() 
      {
      return $this->lastName; 
      }

    function setLastName($lastName)
      {
      $this->lastName = $lastName;
      }

    function getEmailAddress()
      {
      return $this->emailAddress;
      }

    function setEmailAddress($emailAddress)
      {
      $this->emailAddress = $emailAddress;
      }
    }
    ? >

    More Zend Articles
    More By Zend


     

       

    ZEND ARTICLES

    - Taking the Zend Certified PHP Engineer Exam:...
    - Quick Introduction to PHP 5
    - PHP SOAP Extension
    - Improving Performance
    - PDFs with PHP part 2
    - PDFs with PHP part 1
    - PHP at Lycos
    - Build Database Interfaces





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