PHP
  Home arrow PHP arrow Page 4 - 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 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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 6 hosted by Hostway
    Stay green...Green IT