How to get primary key of last inserted row into database?

I’m writing a plugin for WordPress and I have a table with a AUTO_INCREMENT primary key field called ID. When a new row is inserted into the table, I’d like to get the ID value of the insertion. How do I do this?

I ran into this the other day and tried using is a method called mysql_insert_id which is the standard PHP way of doing this.It didn’t quite work though, as my queries were all going through the WordPress class $wpdb and I couldn’t guarantee that my query was the last thing that was being executed.

Well, it turns out that one can use the class varialbe $wpdb->insert_id which is updated with mysql_insert_id when your INSERT is completed. Now that’s easier than finding a Arai Signet-q when you’re really looking for one right?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.