PHP
  Home arrow PHP arrow Page 3 - Using 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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
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

Using PDO Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 16
    2007-05-29

    Table of Contents:
  • Using PDO Objects in PHP 5
  • Using the PDO extension
  • Running queries against a specific database
  • Finding insertion IDs for database rows

  • 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


    Using PDO Objects in PHP 5 - Running queries against a specific database


    (Page 3 of 4 )

    Naturally, after learning how to open and close a connection to a specific database system, I'm sure that you'll want to see how to execute a query against a particular database. To perform this crucial task, the PDO extension comes equipped with two primary methods, not surprisingly called "query()" and "exec()" respectively.

    However, the two methods behave slightly differently. The second one, that is "exec()," not only performs a specified query, but returns the number of affected rows after performing an "INSERT," "UPDATE" or "DELETE" command.

    Now that you see the difference between both methods, please study the following examples. They demonstrate separately how to use each of the methods in question, first for connecting to MySQL, and then for fetching some rows from a sample "USERS" database.

    The respective code listings are as follows:

    // example using the 'query()' method
    try{
                $dbh=new PDO('mysql:host=localhost;dbname=alejandro','user','password');
                foreach($dbh->query('SELECT * FROM users') as $row){
                            echo 'Name :'.$row['name'].' Postal Address :'.$row['address'].' Email :'.$row['email'].'<br />';
                }
    }
    catch(PDOException $e){
                echo 'Error : '.$e->getMessage();
                exit();
    }

    // example using the 'exec()' method (returns the number of rows
    affected by the query)
    try{
                $dbh=new PDO('mysql:host=localhost;dbname=alejandro','user','password');
                $delrows=$dbh->exec('DELETE FROM users WHERE id<20');
                echo 'Number of deleted rows is the
    following :'.$delrows;
    }
    catch(PDOException $e){
                echo 'Error : '.$e->getMessage();
                exit();
    }

    As shown previously, the first example illustrates the implementation of the handy "query()" method to fetch some rows from a simple database table, while the second case shows how to use "exec()" to run a "DELETE" statement, and finally display the number of deleted rows.

    It's worth clarifying here that all of the above examples use a MySQL server, but as you learned in the previous section, the same methods can be utilized with Oracle, MS SQL, SQLite or whatever database system you need to include in your own PHP applications.

    So far, so good. At this stage you have hopefully learned how to run queries with the PDO library utilizing the two primary methods bundled with the extension. So what's the next step? Well, in the last section of this tutorial I'm going to show you how to use a brand new method, called "lastInsertId()," which obviously returns the ID of the last-inserted database row.

    To learn how this method can be implemented, go ahead and read the new few lines. I'll be there, waiting for you.

    More PHP Articles
    More By Alejandro Gervasio


       · This first article of the series introduces the basics on working with the PDO (PHP...
       · You describe PDO as a db abstraction layer in the last line of paragraph 5 after...
       · Thanks you for commenting on my PHP tutorial. Now, with reference to your question...
       · PDO is data-access abstraction (not database abstraction). The primary goal is to...
       · Thank you for posting your opinions here. You’re correct, providing that some...
       · So then maybe I am missing the value in using this ext.Why would you want to use...
       · Thank you again for commenting on my PHP article. Well, while using the PEAR:MDB2 is...
       · Well, thank you for your tutorial,however I think you missed the biggest added...
       · Thank you for commenting on my PHP article. However I'm afraid you're mistaken since...
     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway