PHP
  Home arrow PHP arrow Page 3 - 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 
Moblin 
JMSL Numerical Library 
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 - 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


       · 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

    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP
    - PHP Networking
    - Validating Web Forms with the Code Igniter P...





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