PHP
  Home arrow PHP arrow Database Details and PHP
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

Database Details and PHP
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2007-06-28


    Table of Contents:
  • Database Details and PHP
  • Details About a Query Response
  • Metadata
  • Sample Application

  • 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


    Database Details and PHP
    ( Page 1 of 4 )

    Picking up from where we left off last week, we'll be discussing shortcuts, query responses, metadata, and more. This article is excerpted from chapter eight of the book Programming PHP, Second Edition, written by Kevin Tatroe, Rasmus Lerdorf, and Peter MacIntyre (O'Reilly, 2006; ISBN: 0596006810). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    Shortcuts 

    PEAR DB provides a number of methods that perform a query and fetch the results in one step: getOne(), getRow(), getCol(), getAssoc() , and getAll() . All of these methods permit placeholders.

    The getOne() method fetches the first column of the first row of data returned by an SQL query:

      $value = $db->getOne(SQL [, values ]);

    For example:

      $when = $db->getOne("SELECT avg(pub_year) FROM books");
      if (DB::isError($when)) {
       
    die($when->getMessage());
      }

      echo "The average book in the library was published in $when";

      The average book in the library was published in 1974

    The getRow() method returns the first row of data returned by an SQL query:

      $row = $db->getRow(SQL [, values ]]);

    This is useful if you know only one row will be returned. For example:

      list($title, $author) = $db->getRow(
        "SELECT books.title,authors.name FROM books, authors
         WHERE books.pub_year=1950 AND books.authorid=authors.authorid");
      echo "($title, written by $author)";
      (I, Robot, written by Isaac Asimov)

    The getCol() method returns a single column from the data returned by an SQL query:

      $col = $db->getCol(SQL [, column [, values ]]);

    The column  parameter can be either a number (0, the default, is the first column), or the column name.

    For example, this fetches the names of all the books in the database, ordered by the year they were released:

      $titles = $db->getAll("SELECT title FROM books ORDER BY pub_year ASC");
      foreach ($titles as $title) {
       
    echo "$title\n";
      }
     
    the Hobbit
      I, Robot
      Foundation
      ...

    The getAll() method returns an array of all the rows returned by the query:

      $all = $db->getAll(SQL [, values [, fetchmode ]]);

    For example, the following code builds a select box containing the names of the movies. The ID of the selected movie is submitted as the parameter value.

      $results = $db->getAll("SELECT bookid,title FROM books ORDER BY pub_year ASC") ;
      echo "<select name='movie'>\n";
      foreach ($results as $result) {
        echo "<option value={$result[0]}>{$result[1]}</option>\n";
      }
      echo "</select>";

    All the get*() methods return DB_ERROR when an error occurs.



     
     
    >>> More PHP Articles          >>> More By O'Reilly Media
     

       

    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 6 Hosted by Hostway
    Stay green...Green IT