PHP
  Home arrow PHP arrow Page 2 - 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? 
Google.com  
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 - Details About a Query Response
    ( Page 2 of 4 )

    Four PEAR DB methods provide you with information on a query result object: numRows(), numCols(), affectedRows(), and tableInfo() .

    The numRows() and numCols() methods tell you the number of rows and columns returned from a SELECT query:

      $howmany = $response->numRows();
      $howmany = $response->numCols();

    The affectedRows() method tells you the number of rows affected by an INSERT , DELETE , or UPDATE operation:

      $howmany = $response->affectedRows();

    The tableInfo() method returns detailed information on the type and flags of fields returned from a SELECT operation:

      $info = $response->tableInfo();

    The following code dumps the table information into an HTML table:

      // connect
       require_once('DB.php');
       $db = DB::connect("mysql://librarian:passw0rd@localhost/ library");
       if (DB::iserror($db)) {
        
    die($db->getMessage());
       }

      $sql = "SELECT * FROM BOOKS";

      $q = $db->query($sql);
      if (DB::iserror($q)) {
        die($q->getMessage());
      }

      $info = $q->tableInfo();
      a_to_table($info);

      function a_to_table ($a) {
        echo "<html><head><title> Table Info </title></head>";
        echo "<table border=1>\n";
        foreach ($a as $key => $value) {
         
    echo "<tr valign=top align=left><td>$key</td><td>";
          if (is_array($value)) {
           
    a_to_table($value);
          } else {
           
    print_r($value);
         
    }
         
    echo "</td></tr>\n";
        
    }
        
    echo "</table>\n";
      }

    Figure 8-2 shows the output of the table information dumper.


    Figure 8-2.  The information from tableInfo( )

    Sequences

    Not every RDBMS has the ability to assign unique row IDs, and those that do have wildly differing ways of returning that information. PEAR DB sequences are an alternative to database-specific ID assignment (for instance, MySQL’s AUTO_INCREMENT).

    The nextID() method returns the next ID for the given sequence:

      $id = $db->nextID(sequence);

    Normally you’ll have one sequence per table for which you want unique IDs. This example inserts values into the books table, giving a unique identifier to each row:

      $books = array(array('Foundation', 1951),
                     
    array('Second Foundation', 1953),
                     
    array('Foundation and Empire', 1952));

      foreach ($books as $book) {
       
    $id = $db->nextID('books');
       
    splice($book, 0, 0, $id);
       
    $db->query('INSERT INTO books (bookid,title,pub_year) VALUES (?,?,?)',
    $book);
     
    }

    A sequence is really a table in the database that keeps track of the last-assigned ID. You can explicitly create and destroy sequences with the createSequence() and dropSequence() methods:

      $res = $db->createSequence(sequence);
      $res = $db->dropSequence(sequence);

    The result will be the result object from the create or drop query or DB_ERROR if an error occurred.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek