PHP
  Home arrow PHP arrow Page 3 - Databases 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

Databases and PHP
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2007-06-14


    Table of Contents:
  • Databases and PHP
  • Relational Databases and SQL
  • PEAR DB Basics
  • Data Source Names

  • 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


    Databases and PHP - PEAR DB Basics
    ( Page 3 of 4 )

    Example 8-1 is a program to build an HTML table of information about science fiction books. It demonstrates how to use the PEAR DB library (which comes with PHP) to connect to a database, issue queries, check for errors, and transform the results of queries into HTML. Be sure to verify how to turn on PEAR with your setup of PHP, as the Unix/Linux flavor is slightly different to that of Windows. Go to http:// www.pear.php.net/manual/en/installation.getting.php for a starting point on installation. The library is object-oriented, with a mixture of class methods (DB::connect(), DB::iserror()) and object methods ( $db->query() , $q->fetchInto() ).

    Example 8-1.  Display book information

    <html><head><title>Library Books</title></head>
    <body>

    <table border=1 >
    <tr><th>Book</th><th>Year Published</th><th>Author</th></tr>
    <?php
     // connect
     
    require_once('DB.php');
     
    $db = DB::connect("mysql://librarian:passw0rd@localhost/ library");
     
    if (DB::iserror($db)) {
      
    die($db->getMessage());
     }

     // issue the query
     
    $sql = "SELECT books.title,books.pub_year,authors.name
            
    FROM books, authors
            
    WHERE books.authorid=authors.authorid
            
    ORDER BY books.pub_year ASC";
     $q = $db->query($sql);
     if (DB::iserror($q)) {
       die($q->getMessage());
     }

     // generate the table
     
    while ($q->fetchInto($row)) {
    ?>
    <tr><td><?= $row[0] ?></td>
        <td><?= $row[1] ?></td>
       
    <td><?= $row[2] ?></td>
    </tr>
    <?php
     
    }
    ?>

    The output of Example 8-1 is shown in Figure 8-1.


    Figure 8-1.  The Library page



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