PHP
  Home arrow PHP arrow Page 2 - Handling Result Sets and More with PDO Objects in PHP 5
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

Handling Result Sets and More with PDO Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2007-06-04


    Table of Contents:
  • Handling Result Sets and More with PDO Objects in PHP 5
  • Preparing queries and fetching database rows
  • Fetching database rows and columns
  • Counting affected rows and columns

  • 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


    Handling Result Sets and More with PDO Objects in PHP 5 - Preparing queries and fetching database rows
    ( Page 2 of 4 )

    In consonance with the concepts deployed at the beginning of this article, the next group of PDO-related methods that I plan to teach you are those aimed at preparing/executing queries and fetching database rows.

    But first I'd like to introduce at least basically the concept of "prepared" queries, since I'm going to use it with many of the next hands-on examples. Summarizing, a prepared query can be considered a SQL statement that was previously compiled by the selected database system, allowing the inclusion of parameters inside the query itself, which is properly analyzed, compiled and finally optimized.

    As you can see, this concept can be extremely useful when working with SQL queries that are potentially complex in their definition. Once the query in question has been compiled by the database system, it can be executed repeatedly, without consuming additional computational resources. Pretty efficient, right?

    Now that you hopefully grasped the exact meaning of a "prepared" query, let me show you some basic examples of how to execute this type of SQL statement using the PDO extension.

    In this case, three brand new methods are provided by the extension, called "prepare()," "execute()" and "fetch()" respectively. The first two are aimed obviously at preparing/running a specified SQL query, while the last one is useful to fetch one database row at a time.

    Also, it should be noticed that all the practical examples that I'm going to include in the next few lines won't use parameters inside the corresponding queries, since this feature will be covered profusely in the next tutorial of the series.

    Therefore, at least for now, have a look at the following code samples to learn how to fetch rows from a sample "USERS" database table using the corresponding "fetch()" PDO method.

    Here are the examples:

    // example using the 'fetch()' method with PDO::FETCH_ASSOC
    constant (return database table row as an associative array)
    try{
       $dbh=new PDO('mysql:host=localhost;dbname=alejandro','user','password');
       $dbh->prepare('SELECT * FROM users');
       $dbh->execute();
       $result=$dbh->fetch(PDO::FETCH_ASSOC);
       print_r($result);
    }
    catch(PDOException $e){
       echo 'Error : '.$e->getMessage();
       exit();
    }

    // example using the 'fetch()' method with PDO::FETCH_BOTH
    constant (return database table row as an associative and numeric
    array)
    try{
       $dbh=new PDO('mysql:host=localhost;dbname=alejandro','user','password');
       $dbh->prepare('SELECT * FROM users');
       $dbh->execute();
       $result=$dbh->fetch(PDO::FETCH_BOTH);
       print_r($result);
    }
    catch(PDOException $e){
       echo 'Error : '.$e->getMessage();
       exit();
    } 

    // example using the 'fetch()' method with PDO::FETCH_OBJ
    constant (returns an object with column names as properties)
    try{
       $dbh=new PDO('mysql:host=localhost;dbname=alejandro','user','password');
       $dbh->prepare('SELECT * FROM users');
       $dbh->execute();
       $result=$dbh->fetch(PDO::FETCH_OBJ);
       echo 'Name : '.$result->NAME.' Postal Address : '.$result-
    >ADDRESS.' Email : '.$result->EMAIL;
    }
    catch(PDOException $e){
       echo 'Error : '.$e->getMessage();
       exit();
    }

    As you can see, the three examples shown above first use the "prepare()" and "execute()" methods to run a simple SELECT query against a sample database, and then the returned rows are retrieved via the "fetch()" method that I introduced before.

    Besides, you should notice that in each case I used three different constants, that is PDO::FETCH_ASSOC, PDO::FETCH_BOTH, and PDO::FETCH_OBJ, precisely to fetch database rows as an associative/numeric array, and finally as an object.

    So far, so good. At this point I believe that the previous methods shouldn't be hard to understand at all, since they behave closely similar to the PHP functions included with the MySQL library that you'll certainly have used hundreds of times. Therefore, it's time to move forward and continue exploring more methods that come integrated with the PDO extension.

    And speaking of that, in the section to come, I'm going to show you how to use the PDO library to fetch all the rows contained into a given result set, in addition to manipulating separately its respective columns.

    To see how all these useful tasks can be performed with PDO objects, go ahead and read the next few lines.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek