MySQL
  Home arrow MySQL arrow Page 3 - PHP and Databases for the Lazy Sod
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? 
MYSQL

PHP and Databases for the Lazy Sod
By: Justin Vincent
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2003-01-19

    Table of Contents:
  • PHP and Databases for the Lazy Sod
  • Atomic Operations
  • Use PHP Functions not DB Functions!
  • Abstraction
  • Getting Even Lazier
  • 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


    PHP and Databases for the Lazy Sod - Use PHP Functions not DB Functions!


    (Page 3 of 6 )

    Hopefully by now you are beginning to get the idea of why query result sets are useful.

    The point is this, if you extract your results into an array like the ones shown above, you no longer need to play around with lots of different types of database specific functions in order to work with extracted data.

    The only functions you need to use (99.9% of the time) are PHP functions. The really great thing about this is that you can be darn sure that your code is much more portable between databases.

    Another benefit is that you need so much less code! (Which is, of course, great news if you're a lazy sod like me.)

    Lets look at some 'meat and potato' ways to work with query result sets using built-in PHP functions, bearing in mind that the result sets are in the same format as the ones described above.

    Count how many rows of results have been returned

    echo count($results);

    Print all results

    foreach ( $results as $result )
    {
      echo $result->id;
      echo $result->name;
      echo $result->email;
    }


    Print one row of results

    echo $results[0]->id;
    echo $results[0]->name;
    echo $results[0]->email;


    Print one variable from one row of results

    echo $results[0]->name;

    Re-sort results (sorts on first element of second dimension)

    assort($results); // or any other sort function

    Interlude

    Now that we have defined our main atomic functions and we have a new way of working with query results, we need a nice new code library that turns the standard database gunk into a few neat, atomic functions. If we do this correctly, the only code we will ever need to write again is:

    1. A little bit of code to send a query to the database
    2. A little bit of code to deal with results
    3. And that, my friends, is a lazy sod's dream come true.
    But Where's the Class?

    What we really need is a PHP class that does all of the above and makes it very easy to do so. You guessed it, it just so happens that I've already made one! Of course, you don't have to use it -- you're welcome to make your own -- but for the sake of this article I am going to use it as an example of how to be as lazy as possible when working with databases.

    The class in question is called ezSQL and is available from http://php.justinvincent.com.

    To install it, you'll need to:
    1. Download it.
    2. Change the database settings at the top of it.
    3. Include it in the start of your PHP script.
    You don't need to worry about what ezSQL is actually doing. All you need to know is that it takes care of gunk. It deals with connections, row fetching, freeing space, etc. Let's have a look at how easy it is to print out some results using this new class:

    <?php

    include_once "ez_sql.php";

    $users = $db->get_results("SELECT * FROM users");

    foreach ( $users as $user )
    {
      echo $user->name;
    }

    ?>


    As you can see, the new class takes care of everything gunk related. The connection stuff happens simply by including the class in the first place.

    The only thing we need to do is use one simple function that takes a SQL query as an argument and outputs a query result set. From that point forward, we simply use PHP functions to work with the data.

    More MySQL Articles
    More By Justin Vincent


     

    MYSQL ARTICLES

    - MySQL and BLOBs
    - Two Lessons in ASP and MySQL
    - Lord Of The Strings Part 2
    - Lord Of The Strings Part 1
    - Importing Data into MySQL with Navicat
    - Building a Sustainable Web Site
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - PhpED 3.2 – More Features Than You Can Poke ...
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - Security and Sessions in PHP
    - Setup Your Personal Reminder System Using PHP
    - Create a IP-Country Database Using PERL and ...
    - Developing a Dynamic Document Search in PHP ...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT