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

Finding insertion IDs for database rows - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I pointed out in the section you just read, the PDO extension also offers a handy method, called "lastInsertId()," which is useful in those situations where it's necessary to find out the ID of the last-inserted database row.

The implementation of the method is very straightforward, as you can see in the example below:

// example using the 'lastInsertId()' method (returns the ID of
last inserted row)
try{
            $dbh=new PDO('mysql:host=localhost;dbname=alejandro','user','password');
            $dbh->query("INSERT INTO users SET
name='Alejandro',address='Nowhere',email=
'alejandro@domain.com'");
            $insertId=$dbh->lastInsertId();
            echo 'ID of last-inserted row after executing SQL
statement is as following: '.$insertId;
}
catch(PDOException $e) {
            echo 'Error : '.$e->getMessage();
            exit();
}

Finding the ID of the last-inserted database row is an easy-to-perform task, thanks to the excellent functionality provided by the "lastInsertId()" method. Similar to the approach followed with previous examples, in this case I used the MySQL server to demonstrate how this method works, but as you saw earlier, this condition can be easily modified to work with a different database system.

As usual with many of my articles on PHP development, feel free to introduce your own modifications to all the hands-on examples shown here, so you can acquire a more robust grounding in how to use the most important features offered by the PDO extension. Fun is already guaranteed!

Final thoughts

In this first part of the series, I walked you through the key points of how to use the PDO extension that comes bundled with 5.1 and up. As was demonstrated by the hands-on examples included in this article, this library definitely makes working with multiple database systems a painless process.

Nonetheless, I have to admit that I'm only scratching the surface when it comes to exploring the numerous features offered by PHP Data Objects. So, considering the long way ahead of us, in the next tutorial I'm going to show you how to use this powerful PHP extension to manipulate results sets regardless of the database system you use.

Now that you've been warned, you won't want to miss it!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: