PHP
  Home arrow PHP arrow Page 2 - Working with Prepared Queries with PDO...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Working with Prepared Queries with PDO Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · Over the course of this second tutorial of the series, you'll learn how to work with...
       · $dbh->prepare('SELECT * FROM users WHERE name=? AND...
       · Thank you for your pointing me out that small bug on my PHP example. The PDO code...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT