Home arrow PHP arrow Page 3 - Using PDO Objects in PHP 5

Running queries against a specific database - PHP

The PDO library is a powerful addition to PHP. It shines when you need to build applications capable of talking to different database systems. It also has other very useful capabilities. This article, the first in a three-part series, will introduce you to this versatile library.

  1. Using PDO Objects in PHP 5
  2. Using the PDO extension
  3. Running queries against a specific database
  4. Finding insertion IDs for database rows
By: Alejandro Gervasio
Rating: starstarstarstarstar / 32
May 29, 2007

print this article



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
            $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();

// example using the 'exec()' method (returns the number of rows
affected by the query)
            $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();

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: