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

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

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?


blog comments powered by Disqus
PHP ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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