PHP
  Home arrow PHP arrow Page 5 - Accessing Databases with DB
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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

Accessing Databases with DB
By: David Sklar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 13
    2004-11-30


    Table of Contents:
  • Accessing Databases with DB
  • Introducing DSNs
  • Understanding Quoting and Placeholders
  • Examining Data Retrieval Convenience Methods
  • Understanding Query Information
  • Running a Query Multiple Times
  • Introducing Sequences

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Accessing Databases with DB - Understanding Query Information
    ( Page 5 of 7 )

    In addition to methods that send queries to the database and return results, DB provides some methods that return information about a query. These methods tell you the size of the result set returned or modified by a query.

    DB_Result::numRows()

    The numRows() method returns the number of rows in a result set. This is useful for checking if any rows were selected before printing them:

    $res = $dbh->query('SELECT flavor FROM ice_cream
           WHERE price < 5');
    if ($res->numRows() > 0) {
         print "Your choices: <ul>";
         while($row = $res->fetchRow())
              { print "<li> $row[0]</li>"; }
         print "</ul>";
    } else { 
         print "No flavors available for less than five
                dollars.";
    }

    The numRows() method is called on the statement handle, not on the database handle. You can only call numRows() after a SELECT query.

    DB_Result::numCols()

    The numCols() method returns the number of columns in a result set. You can use numCols() to dynamically display information about a table:

    $res = $dbh->query('SELECT * FROM ice_cream');
    print 'There are '.$res->numCols().' columns in the
           ice_cream table.'; print '<table>';
    while($row = $res->fetchRow()) {
         print '<tr>';
         foreach ($row as $val) { print "<td>$val</td>"; }
         print '</tr>';
    }
    print '</table>';

    Like numRows(), the numCols() method is called on the statement handle, not on the database handle. You can only call numCols() after a SELECT query.

    DB::affectedRows()

    The affectedRows() method returns how many rows were changed by an UPDATE, INSERT, or DELETE query. It is called on the database handle, not the statement handle. This is because the query() method doesn’t return a statement handle for these kinds of queries, just a status code or error object:

    $dbh->query("UPDATE ice_cream SET price = price - 1 WHERE
          flavor
    LIKE 'Chocolate%'");
    print 'Discount applied to ' . $dbh->affectedRows()
         
    . ' Chocolate flavors.';

    This chapter is from Essential PHP Modules, Extensions, Tools, by David Sklar (Apress, 2004, ISBN: 1590592808). Check it out at your favorite bookstore today.

    Buy this book now.



     
     
    >>> More PHP Articles          >>> More By David Sklar
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT