PHP
  Home arrow PHP arrow Page 2 - Working with Prepared Queries 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

Working with Prepared Queries with PDO Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2007-06-11


    Table of Contents:
  • Working with Prepared Queries with PDO Objects in PHP 5
  • Working with prepared queries
  • Using an alternate approach with prepared queries
  • Working with transactions

  • 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


    Working with Prepared Queries with PDO Objects in PHP 5 - Working with prepared queries
    ( Page 2 of 4 )

    Before I proceed to show you the practical examples concerning the implementation of a prepared query, I'd like to remind you quickly of its intrinsic definition. In layman's terms, a prepared query can be thought of as a SQL statement that is 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 feature can be quite useful in those cases where complex statements must be executed against one or more databases, since the respective commands will be performed significantly faster.

    All right, now that you're familiar with the definition of a prepared query, let me show you an example in which this type of query is executed along with a couple of named parameters. The example in question is as follows:

    // example using the 'prepare()' method with named parameters
    try{
      
    $dbh=new PDO
    ('mysql:host=localhost;dbname=alejandro','user','password');
      
    $dbh->prepare('SELECT * FROM users WHERE name=:name AND
    email=:email');
       $dbh->execute(array
    (':name'=>'Alejandro',':email'=>'alejandro@domain.com'));
      
    $result=$dbh->fetchAll();
      
    // displays data for 'Alejandro'
      
    print_r($result);
      
    $dbh->execute(array
    (':name'=>'John',':email'=>'john@domain.com'));
      
    // display data for 'John'
      
    print_r($result);
    }
    catch(PDOException $e) {
      
    echo 'Error : '.$e->getMessage();
      
    exit();
    }

    As you can see, the previous example demonstrates in a friendly fashion how to run a prepared query with a pair of named parameters associated with it. More specifically speaking, in this case I used the already familiar "prepare()" and "execute()" PDO methods, which were covered in detail in the first article of the series, to run two different SQL statements, even though the query in question has been compiled only once.

    Since the "name" and "email" parameters have been attached to the initial query, they're used to fetch fictional data from a sample "USERS" database table. Quite easy to understand, right?

    However, if the previous example illustrates the implementation of a prepared query with named parameters, now let me show you another one that shows how to apply the same concept, this time using question marks.

    The pertinent code sample is as follows:

    // example using the 'prepare()' method with (?) question mark
    parameters
    try{
      
    $dbh=new PDO('mysql:host=localhost;dbname=alejandro','user','password');
      
    $dbh->prepare('SELECT * FROM users WHERE name=? AND email=?');
      
    $dbh->execute(array('Alejandro','alejandro@domain.com'));
      
    $result=$dbh->fetchAll();
      
    // displays data for 'Alejandro'
      
    print_r($result);
      
    $dbh->execute(array('John','john@domain.com'));
      
    // display data for 'John'
      
    print_r($result);
    }
    catch(PDOException $e) {
      
    echo 'Error : '.$e->getMessage();
      
    exit();
    }

    As shown above, using question marks instead of named parameters to execute a prepared query is a no-brainer process that can be performed with minor hassles. In this case I also fetched the same database rows that you saw in the previous example, but obviously the question marks have been replaced with real data.

    So far, so good. At this stage you hopefully grasped the logic that stands behind the concept of running prepared queries. However, the PDO extension comes equipped with another handy method that can be used for performing this specific type of query as well (and others, by the way). It's called "bindParam()," and it deserves a closer look.

    In the following section I'm going to show you how to use this brand new method, thus click on the link below and keep reading.



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