PHP
  Home arrow PHP arrow 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
    ( Page 1 of 7 )

    This chapter discusses the database abstraction layer PEAR DB. This package supplies a standard set of functions for talking to many different kinds of databases (from Essential PHP Modules, Extensions, Tools, by David Sklar, 2004, Apress, ISBN: 1590592808).

    phpPEAR DB provides a consistent set of methods for using a relational database no matter what database you’re using. It supports the following PHP database extensions: FrontBase, Informix, InterBase, Microsoft SQL Server, mSQL, ODBC, MySQL, Sybase, OCI8, and PostgreSQL. A driver that supports new features in version 4 of MySQL is also in the works. This chapter covers DB version 1.4.

    Exploring a Simple DB Example

    Retrieving a result and displaying it in a table with DB looks like this:

    // Load the DB code
    require 'DB.php';

    // Connect to the database
    $dbh = DB::connect('mysql://user:password@host/database');

    // Send a SELECT query to the database
    $sth = $dbh->query('SELECT flavor, price, calories FROM ice_cream');

    // Check if any rows were returned
    if ($sth->numRows()) {
         print "<table>"; 
         print "<tr><th>Ice Cream Flavor</th><th>Price per Serving</th><th>Calories per Serving</th></tr>";
         // Retrieve each row 
         while ($row = $sth->fetchRow()) {
               // And print out the elements in the row
               print "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2</td></tr>\n";
         }
         print "</table>";
    } else {
         print "No results";
    }

    DB::connect() is a static class method that returns an object. You interact with the database by calling methods on this object. In this example, the object is assigned to the variable $dbh, which stands for database handle. The argument to DB::connect() is a Data Source Name (DSN). DSNs are explained in the next section.

    Next, you send a query to the database server with the $dbh->query() method. This method returns a statement handle that is assigned to $sth. This variable is a DB_Result object with methods that return information about the retrieved rows. The $sth->numRows() method returns the number of retrieved rows. In this example, it’s used to determine whether to print the retrieved data or just a message saying that no data was retrieved. The “Query Information” section explains methods such as numRows() that provide information about a query.

    The $sth->fetchRow() method returns an array that contains one row of data retrieved from the database. The statement handle maintains an internal counter of what the “next” row to return is, so the first time you call fetchRow(), you get the first row retrieved from the database; the second time you call fetchRow(), you get the second row, and so on. After all retrieved rows have been returned, fetchRow() returns NULL instead of a result array. This makes it easy to use in a while() loop as the example does.

    The fetchRow() method returns an indexed array with retrieved data. The first field requested by the SELECT query is in element 0 of the array, the second field in element 1, and so on. In this example, $row[0] is the value of the flavor column in each row, $row[1] is the price column, and $row[2] is the calories column. You can also retrieve rows as associative arrays or objects. The “Sending Queries and Retrieving Results” section discusses fetchRow() in more detail.

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