MySQL
  Home arrow MySQL arrow Page 4 - 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 - Abstraction


    (Page 4 of 6 )

    It's not hard to see how this makes it very easy to port your code from one database to another (as long as you use ANSI SQL in your database queries). In the ezSQL class itself there are only three functions that are database specific.

    I have already ported the class from mySQL to Oracle8. It took me about 30 minutes to change the three functions that needed changing. This means that I can run exactly the same script using either an Oracle8 database or a mySQL database. All I have to do is to replace the include file ez_sql.php.

    A more obvious example might be:

    <?php

    if ( $server == "oracle8" )
      include_once "oracle8/ez_sql.php";
    else
      include_once "mySQL/ez_sql.php";

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

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

    ?>


    Functions You Might Need

    When I wrote ezSQL, the atomic functions I included were:

    $db->get_results - Get multiple row result set from the database
    $db->get_row - Get one row from the database
    $db->get_col - Get one column from query based on column offset
    $db->get_var - Get one variable, from one row, from the database
    $db->query - Send a query to the database (and if any results, cache them)
    $db->debug - Print last SQL query and returned results (if any)
    $db->vardump - Print the contents and structure of any variable
    $db->select - Select a new database to work with
    $db->get_col_info - Get information about columns such as column name
    $db = new db - Initiate new db object

    The only database specific functions are $db->query, $db->select and $db->db (the constructor function). All other functions use standard PHP code only.

    The other point to note is that the two main functions that return rows of results take an optional argument to specify whether to return the results as an associative array, numerical array or object (which is the default). So, now let's have a look at a few more useful ways to work with this new class.

    Say I want to validate a user's password against a password stored in the database.

    I could do this:

    if ( $pwd == $db->get_var("SELECT pwd FROM users WHERE id = 2") )
    {
      echo "You are logged in!";
    }
    else
    {
      echo "Sorry. Bad user name or bad password.";
    }


    But wait, we can go one better. This time we can check the password while at the same time pulling in extra user details that we can use if the password is valid:

    if ( $pwd == $db->get_var("SELECT pwd, name, id FROM users WHERE id = 2") )
    {
      $user = $db->get_row(null);

      echo "Hello $user->name your ID is $user->id and you are now logged in!";
    }
    else
    {
      echo "Sorry. Bad user name or bad password.";
    }


    Neat! There are a number of reasons why this works.
    1. The function $db->get_var() always returns the variable that is stored in the first column of the first row of the results (unless otherwise specified).
    2. Even though we have only extracted one variable using $db->get_var(), the query itself asked for three columns of information. The full results have been cached as a query result set within the db object, ready for any other ezSQL function to use.
    3. We have taken full advantage of this caching technique by using the function $db->get_row with a null query. This executes the main code of the function, but instead of getting the results from the database it gets the results from the previously cached result set.

    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