PHP
  Home arrow PHP arrow Page 4 - 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 transactions
    ( Page 4 of 4 )

    Any database abstraction layer that fits the requirements of modern web applications must support transactions. The PDO extension is no exception. It comes packaged with a neat set of methods aimed specifically at dealing with this important feature.

    Basically, the library includes three simple methods for working with transactions, called "beginTransaction()," "commit()" and "rollBack()" respectively. All of them are very easy to follow. Below I coded two hands-on examples that demonstrate their functionality, at least primitively.

    Here are the corresponding code samples:

    // example using 'beginTransaction()', 'rollback()' and 'commit
    ()' methods
    try{
      
    $dbh=new PDO('mysql:host=localhost;dbname=alejandro','user','password');
      
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      
    $dbh->beginTransaction();
      
    $dbh->exec("INSERT INTO users (id,name,address,email) VALUES
    (NULL,'Lindsay Wagner','1234 Binary
    Avenue','lindsay@domain.com')");
      
    $dbh->exec("INSERT INTO users (id,name,address,email) VALUES
    (NULL,'Jeff Wilson','666 Parsed Boulevard','jeff@domain.com')");
      
    $dbh->commit();
    }
    catch(PDOException $e) {
      
    $dbh->rollBack();
      
    echo 'Error : '.$e->getMessage();
    }

    // additional example using 'beginTransaction()', 'rollback()'
    and 'commit()' methods
    try{
      
    $dbh=new PDO
    ('mysql:host=localhost;dbname=alejandro','user','password');
      
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      
    $dbh->beginTransaction();
      
    $dbh->exec("INSERT INTO users (id,name,address,email) VALUES
    (NULL,'Susan Norton','1234 Jointner
    Avenue','lindsay@domain.com')");
      
    $dbh->exec("UPDATE users SET name='Jim Smith',address='1010
    Main Street',email='jim@domain.com' WHERE id=5");
      
    $dbh->commit();
    }
    catch(PDOException $e) {
      
    $dbh->rollBack();
      
    echo 'Error : '.$e->getMessage();
    }

    As you can see, the above hands-on examples utilize the group of transaction-related methods that I introduced previously, to execute different queries. In the first case, two insertions are performed against the same "USERS" database table that you saw in earlier sections, while in the second case a new row is added to the table in question, and then an existing record is updated. Quite simple, isn't it?

    All right, at this moment you should have a better idea of how to use the PDO extension that comes bundled with PHP 5.1 and up. As you saw, it features plenty of handy methods that can be used with a decent variety of database systems.

    Naturally, you have the liberty to experiment with all the code samples shown here, with the firm purpose of improving your background in this powerful PHP-based database abstraction layer.

    Final thoughts

    Sadly, we've come to the end of this series. As you hopefully learned, the PDO extension can be really helpful if you're developing web applications that talk to different database systems.

    Also, it's fair to say that this series is intended to be a simple introduction to the library's main features, and certainly doesn't cover other additional characteristics. If you're looking for thorough documentation on it, the PHP official site is the best place to go.

    See you in the next PHP tutorial!



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