PHP
  Home arrow PHP arrow Page 3 - 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 - Using an alternate approach with prepared queries
    ( Page 3 of 4 )

    As I expressed in the previous section, the PDO library has another helpful method, called "bindParam()," which can also be used to execute a prepared query. As its name suggests, the method attaches a specified argument to a given SQL statement, following a similar approach to the one shown with named parameters and question marks.

    To show more clearly how the "bindParam()" method works, a basic implementation of it is listed below:

    // example using 'bindParam()' method
    try{
      
    $dbh=new PDO('mysql:host=localhost;dbname=alejandro','user','password');
      
    $sql=$dbh->prepare("INSERT INTO USERS(name,email) VALUES
    (:name,:email)");
      
    $sql->bindParam(':name',$name);
      
    $sql->bindParam(':email',$email);
      
    // assign values for bound parameters and insert new row
      
    $name='John Doe';
      
    $email='john@domain.com';
      
    $dbh->exec();
      
    // assign different values for bound parameters and insert new
    row
      
    $name='Mary Jackson';
      
    $email='mary@domain.com';
      
    $dbh->exec();
    }
    catch(PDOException $e) {
       
    $dbh->rollBack();
      
    echo 'Error : '.$e->getMessage();
    } 

    As you can see, in this case the previous "bindParam()" method is used to associate a couple of parameters with a prepared query, which is a process that should already be familiar to you. However, it should be noticed that in this case the "prepare()" method first returns an "$sql" query object, which is then used to invoke "bindParam()."

    Taking into account that the above example uses the aforementioned "bindParam()" method with named parameters, below I coded a similar script, but in this occasion utilizing question marks.

    Here is corresponding signature for the example in question:

    // example using 'bindParam()' method with numeric values

    try{
      
    $dbh=new PDO('mysql:host=localhost;dbname=alejandro','user','password');
      
    $sql=$dbh->prepare("INSERT INTO USERS(name,email) VALUES
    (?,?)");
      
    $sql->bindParam(1,$name);
      
    $sql->bindParam(2,$email);
      
    // assign values to bound parameters and insert new row
      
    $name='John Doe';
      
    $email='john@domain.com';
      
    $dbh->exec();
      
    // assign different values to bound parameters and insert new
    row
      
    $name='Mary Jackson';
      
    $email='mary@domain.com';
       $dbh->exec();
    }
    catch(PDOException $e) {
      
    $dbh->rollBack();
      
    echo 'Error : '.$e->getMessage();
    }

    After studying the previous example, certainly you'll have to agree with me that running prepared queries with the methods bundled with the PDO extension is an effortless process which can be performed by a few lines of code.

    Nonetheless, it's fair to say that all the code samples listed above also handle the eventual PDO exceptions via a brand new method called "rollBack()." In simple terms, this implies that, if an error occurs when connecting to the specified database system, or when running a query, etc., the system will go back to its initial state.

    As you may have guessed, "rolling back" a certain SQL operation can be achieved by using transactions, a feature that is also supported by the PDO extension. In the following section (the last of this series, by the way), I'm going to show you a couple of examples of how to use the transactional model with PDO objects.

    To learn more on this useful topic, go ahead and read the next few lines. I'll be there, waiting for you.



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