Home arrow PHP arrow Page 5 - Code 10x More Efficiently Using Data Access Objects: Part 1

Code 10x More Efficiently Using Data Access Objects: Part 1

Learn how to efficiently access database information using Data Access Objects (DAO) with PHP. Oto shows us how easy database manipulation can be, as well as how much time can be saved.

Author Info:
By: Oto Hlincik
Rating: 4 stars4 stars4 stars4 stars4 stars / 51
October 11, 2003
  1. · Code 10x More Efficiently Using Data Access Objects: Part 1
  2. · The Old Way
  3. · Code Samples, The Old Way
  4. · Database DAO to the Rescue
  5. · Convenience and Performance
  6. · Conclusion

print this article

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…

$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…

$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.

blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials