PHP
  Home arrow PHP arrow Page 5 - Code 10x More Efficiently Using Data Acces...
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  
Mobile Linux 
App Generation ROI 
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 / 45
    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


    Code 10x More Efficiently Using Data Access Objects: Part 1 - Convenience and Performance


    (Page 5 of 6 )

    Perhaps the nicest thing about using the database DAO is that it increases coding productivity through convenience and at the same time it allows your scripts to perform well. Now, let's look at how would we use the MySQL DAO in a script where we need to retrieve multiple data chunks from the database…

    <?php
    require("core/mysqladapter.phpclass");
    $objDB = new MySQLAdapter("localhost;contact_db;user;password");

    // get the total number of records in the table
    $total_contacts = $objDB->getValue("SELECT COUNT(*) FROM Contacts");

    // get the record for the contact with id 15
    $record = $objDB->getRecord("SELECT * FROM Contacts WHERE id=15");

    // get the records whose first name is "John"
    $records = $objDB->getRecords("SELECT * FROM Contacts WHERE first_name='John'");
    ?>

    Here we have just retrieved three chunks of data from the database, stored them in variables and they are ready to be processed in a template that will produce a nice output. This is what I call increased coding productivity through convenience. And that is not all. The above code will actually perform very well due to a built in feature of the DAO, which lets it reuse the open database connection. Here is how it works. When the data access object is instantiated, the connection string is stored as an internal class variable. The connection to the database is not actually established until the first method is called. After the first method call, the connection is reused for all of the successive calls to the database, substantially decreasing the communication overhead and consequently increasing performance. A frequent question might be: Is my script execution time going to increase when using the MySQL DAO? We have benchmarked the MySQL DAO and found the difference in performance compared to traditional database access insignificant if any.

    Another great feature of the DAO is the ability to call the methods on a different database without the need to instantiate another data access object. You can simply call a method and supply an optional second parameter which is a connection string. In this case, the connection stored in the object is not reused, but rather a new, temporary connection is established to the database specified by the supplied connection string. Here is an example of how this would work…

    <?php
    require("core/mysqladapter.phpclass");
    $objDB = new MySQLAdapter("localhost;db1;user;password");
    $row1 = $objDB->getRecord($query1);
    $row2 = $objDB->getRecord($query2,"localhost;db2;user;password");
    ?>

    The $row1 is returned from db1 while $row2 is returned from db2. The connection to db1 is kept open and is ready to be reused in the remainder of the script, while the connection to db2 is closed immediately after the $query2 is executed. The connection to db2 is closed automatically at the end of the script execution, or optionally it can be closed explicitly by calling the "close()" method.

    More PHP Articles
    More By Oto Hlincik


       · Could you please verify that link. It seems the domain isn't active (anymore)? Would...
       · The link: http://eof.nlcnet.net/core/MySQLAdapter.zipis bad and has been for...
     

    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 1 hosted by Hostway
    Stay green...Green IT