SunQuest
 
       PHP
  Home arrow PHP arrow Page 3 - Code 10x More Efficiently Using Data Acces...
IBM developerWorks
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
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? 
PHP

Code 10x More Efficiently Using Data Access Objects: Part 1
By: Oto Hlincik
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 44
    2003-10-11

    Table of Contents:
  • Code 10x More Efficiently Using Data Access Objects: Part 1
  • The Old Way
  • Code Samples, The Old Way
  • Database DAO to the Rescue
  • Convenience and Performance
  • Conclusion

  • 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

    Code 10x More Efficiently Using Data Access Objects: Part 1 - Code Samples, The Old Way


    (Page 3 of 6 )

    OK, with the logistics out of the way, let's get the total number of contacts currently in the table. To do this the old way, we would need to do something like this…

    <?php
    // get the total number of records in the table
    $hostname = "localhost";
    $username = "user";
    $password = "password";
    $databasename = "contact_db";

    $query="SELECT COUNT(*) FROM Contacts"
    $connection = @ mysql_connect($hostname, $username,$password)
     or die("Cannot connect to database");
    if (! mysql_selectdb($databasename, $connection))
     die("Cannot open table");
    if (!($result = @ mysql_query($strsql, $connection)))
     die("Cannot select record");
    $row = mysql_fetch_row($result);
    $total_contacts = $row[0];
    echo "Total contacts: ".$total_contacts;
    ?>

    Ok, so it’s not as bad you may think. So let’s see how would we go about getting a record for the contact with id 15…

    <?php
    // get the record for the contact with id 15
    $hostname = "localhost";
    $username = "user";
    $password = "password";
    $databasename = "contact_db";

    $query="SELECT * FROM Contacts WHERE id=15"
    $connection = @ mysql_connect($hostname, $username,$password)
     or die("Cannot connect to database");
    if (! mysql_selectdb($databasename, $connection))
     die("Cannot open table");
    if (!($result = @ mysql_query($strsql, $connection)))
     die("Cannot select record");
    $row = mysql_fetch_array($result);
    echo "Contact with id 15...<br>";
    echo "Name: ".$row['first_name']." ".$row['last_name']."<br>";
    echo "Phone: ".$row['phone']."<br>";
    echo "Email: ".$row['email'];
    ?>

    Hm… That wasn't so bad either. But, here comes the fun. Let's get all the contacts whose first name are "John"…

    <?php
    // get the records whose first name is "John"
    $hostname = "localhost";
    $username = "user";
    $password = "password";
    $databasename = "contact_db";

    $query="SELECT * FROM Contacts WHERE first_name='John'"
    $connection = @ mysql_connect($hostname, $username,$password)
     or die("Cannot connect to database");
    if (! mysql_selectdb($databasename, $connection))
     die("Cannot open table");
    if (!($result = @ mysql_query($strsql, $connection)))
     die("Cannot select record");
    echo "Contacts whose first name is 'John'<br>";
    while($row = mysql_fetch_array($result)){
    $row = mysql_fetch_array($result);
    echo "Name: ".$row['first_name']." ".$row['last_name']."<br>";
    echo "Phone: ".$row['phone']."<br>";
    echo "Email: ".$row['email']."<br>";
    }
    ?>

    Well, that is still not so bad. However, what if I wanted to format the results in a nice way? To do that we would need to intersperse the code that returns the data from database with a lot of formatting code, and that leads to very sloppy code. We could possibly retrieve the entire recordset and pass it to a template which will make it look pretty, but again, we would need to add more code. What if there were no records returned? We would need to check for this and add some decision mechanism inside the code that communicates with the database. A good programming practice is to separate the data access layer of the program from the data manipulation and presentation layers. By doing that the business logic of your script is not intermixed with architecture specific, low level statements, such as native PHP database access functions. The data access and data processing separation is quite difficult and awkward without the use of some data access encapsulation.

    With that said, how can we do these three tasks of returning a value, a record, and a recordset from the Contacts table nicer, neater, easier, and simply better?

    More PHP Articles
    More By Oto Hlincik


       · Could you please verify that link. It seems the domain isn't active (anymore)? Would...
     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






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