PHP
  Home arrow PHP arrow Page 6 - Database Abstraction With PHP
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
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

Database Abstraction With PHP
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 49
    2002-02-13

    Table of Contents:
  • Database Abstraction With PHP
  • Alphabet Soup
  • Sultans Of Swing
  • Independence Day
  • Different Strokes
  • The Number Game
  • Preparing For The Long Haul
  • Commitment Issues
  • No News Is Good News
  • Catch Me If You Can
  • Once Again, The Headlines

  • 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    Database Abstraction With PHP - The Number Game
    (Page 6 of 11 )

    The numRows() and numCols() methods can be used to obtain the number of rows and columns in the returned resultset respectively:

    <?php // uncomment this to see plaintext output in your browser // header("Content-Type: text/plain"); // include the DB abstraction layer include("DB.php"); // connect to the database $dbh = DB::connect("mysql://john:doe@localhost/db287"); // execute query $query = "SELECT * FROM cds"; $result = $dbh->query($query); // print number of rows and columns in resultset echo "Query returned " . $result->numRows() . " rows of " . $result->numCols() . " columns each"; // close database connection $dbh->disconnect(); ?>
    The LimitQuery() method can be used to restrict the number of rows retrieved:

    <?php // uncomment this to see plaintext output in your browser // header("Content-Type: text/plain"); // include the DB abstraction layer include("DB.php"); // set some variables for limited query $start = 2; $num = 4; // connect to the database $dbh = DB::connect("mysql://john:doe@localhost/db287"); // execute limited query $query = "SELECT track FROM tracks"; $result = $dbh->LimitQuery($query, $start, $num); echo "[Retrieving rows $start through " . ($start+$num) . " of resultset]\n"; // iterate through rows and print column data // in the form TITLE - ARTIST while($row = $result->fetchRow()) { echo "$row[0]\n"; } // close database connection $dbh->disconnect(); ?>
    In this case, the LimitQuery() method can be used to obtain a subset of the complete resultset retrieved from the database. The first argument to the method is the query to execute, the second is the row offset from which to begin, and the third is the number of rows required.

    If you're feeling voyeuristic, the tableInfo() method can be used to take a quick peek at the structure of the table(s) returned by your query. Consider the following example,

    <?php // uncomment this to see plaintext output in your browser // header("Content-Type: text/plain"); // include the DB abstraction layer include("DB.php"); // connect to the database $dbh = DB::connect("mysql://john:doe@localhost/db287"); // execute query $query = "SELECT title, artist, track FROM cds, tracks WHERE cds.id = tracks.cd"; $result = $dbh->query($query); // get info on structure of tables used in query // this is returned as an array of arrays // dump it with print_r()! print_r($result->tableInfo()); // close database connection $dbh->disconnect(); ?> And then take a look at the output of the tableInfo() command, as seen through the print_r() function: Array ( [0] => Array ( [table] => cds [name] => title [type] => string [len] => 255 [flags] => not_null ) [1] => Array ( [table] => cds [name] => artist [type] => string [len] => 255 [flags] => not_null ) [2] => Array ( [table] => tracks [name] => track [type] => string [len] => 255 [flags] => not_null ) )
    As you can see, information on the table structure - the field names, data types, flags et al - is returned by tableInfo() as an array. Every element of this array corresponds to a column in the resultset, and is itself structured as an associative array. Note that this method only works if your query actually returns a valid resultset - so you can't use it with INSERT or UPDATE queries. Finally, the free() method is used to free the resources associated with a particular resultset.

     <?php // uncomment this to see 
    plaintext output in your browser // header("Content-Type: text/plain"); // 
    include the DB abstraction layer include("DB.php"); // connect to the database 
    $dbh DB::connect("mysql://john:doe@localhost/db287"); // execute query $query 
    "SELECT * FROM cds"$result $dbh->query($query); // iterate through rows 
    and print column data // in the form TITLE - ARTIST while($row = 
    $result->fetchRow()) { echo "$row[1] - $row[2]n"; } // free resultset 
    $result->free(); // close database connection $dbh->disconnect(); 



    Just out of curiosity, look what happens if you use free() in the wrong place.

    <?php // uncomment this to see plaintext output in your browser // header("Content-Type: text/plain"); // include the DB abstraction layer include("DB.php"); // connect to the database $dbh = DB::connect("mysql://john:doe@localhost/db287"); // execute query $query = "SELECT * FROM cds"; $result = $dbh->query($query); // iterate through rows and print column data // in the form TITLE - ARTIST while($row = $result->fetchRow()) { echo "$row[1] - $row[2]\n"; // free resultset $result->free(); } // close database connection $dbh->disconnect(); ?>
    In this case, the "while" loop will execute only once, since the resultset gets free()d on the first iteration of the loop. Consequently, only one record will be displayed as output.

    This is kinda pointless - after all, if you only wanted to display a single record, you wouldn't need a "while" loop in the first place - but interesting to try out; it serves as both a warning to newbies and a source of amusement to more experienced geeks.

    More PHP Articles
    More By icarus, (c) Melonfire


     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway