Home arrow Zend arrow Page 2 - Build Database Interfaces

Replacement Example - Zend

This article is about using and building Database Interfaces (DB interfaces). DB interfaces make application development faster and easier. This is accomplished by relocating scattered DML (Insert, Update, Delete) statements into a single shared tool, the Class. A Class encapsulates data post processing, integrity, and security into a single tool that can be accessed throughout the application.

TABLE OF CONTENTS:
  1. Build Database Interfaces
  2. Replacement Example
  3. Lessen the Impact
  4. Autonomous
By: Zend
Rating: starstarstarstarstar / 10
January 04, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Here is an example in which a database interface has replaced an embedded DML operation:

<?php 
/* Update the client's contact information with the data from $_POST */ 
$userID     = (int) $_POST['userID'];
$email      
trim(addslashes($_POST['email']));
$firstname  
trim(addslashes($_POST['firstname'])); 
$lastname   
trim(addslashes($_POST['lastname'])); 
$address1   
trim(addslashes($_POST['address1'])); 
$address2   
trim(addslashes($_POST['address2'])); 
$city       
trim(addslashes($_POST['city'])); 
$province   
trim(addslashes($_POST['province'])); 
$country    
trim(addslashes($_POST['country' ])); 
$DML 'UPDATE clients SET '
       
"email     = '$email',"
       
"firstname = '$firstname',"
       
"lastname  = '$lastname',"
       
"address1  = '$address1',"
       
"address2  = '$address2',"
       
"city      = '$city',"
       
"province  = '$province',"
       
"country   = '$country',"
       
"WHERE userID=$userID"
if 
($db->Execute($DML)) 

    
// do error handling 

? > 
<?php 
$client 
= new Client(); 
$client
->setUserID    $_POST['userID'   ); 
$client
->setEmail     $_POST['email'    ); 
$client
->setFirstname $_POST['firstname'); 
$client
->setLastname  $_POST['lastname' ); 
$client
->setAddress1  $_POST['address1' ); 
$client
->setAddress2  $_POST['address2' ); 
$client
->setCity      $_POST['city'     ); 
$client
->setProvince  $_POST['province' ); 
$client
->setCountry   $_POST['country'  ); 
if (
$client->submit($db) !== true

    
// do error handling 

? > 


The example above demonstrates a reduction in Application Logic. The DB interface handles all communications with the database, and the programmer can focus on the success or failure of the operation. Using less code to do the same task is most beneficial when there are many parts of the application performing the same DML operations. Rather than duplicating DML queries, you simply use the necessary database interface.



 
 
>>> More Zend Articles          >>> More By Zend
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: