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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      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


    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(), andgetAll(). All of these methods permit placeholders.

    ThegetOne()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

    ThegetRow()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 ]]);

    Thecolumn  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 theget*()methods returnDB_ERRORwhen an error occurs.

    More PHP Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming PHP, Second Edition,"...
     

    Buy this book now. 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). Check it out today at your favorite bookstore. Buy this book now.

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





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