Jump to content


Photo

Obtain new id of new item added to mysql table


  • Please log in to reply
3 replies to this topic

#1 jasonc

jasonc
  • Members
  • PipPipPip
  • Advanced Member
  • 841 posts

Posted 09 June 2006 - 09:12 PM


I wish to find out how i can get the id of the most recent item added to the mysql database

The ID is set by the mysql database by increasing by one to the last entry.

I need to find the best way to find out what this number is.

can someone please advise how this can be done.

thank you in advance
Edit/Delete Message

#2 yonta

yonta
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 09 June 2006 - 09:46 PM

Just after the insert query do a call like this:

$recordid = mysql_insert_id();

Check it out [a href=\"http://pt.php.net/mysql_insert_id\" target=\"_blank\"]here[/a]



do it, do it right, do it right now

#3 radalin

radalin
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 10 June 2006 - 08:11 AM

if you use pear::db or pear::mdb2 you can use nextID() method of db class
Roy Simkes
Yet Another Parkyeri Developer

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 10 June 2006 - 08:19 AM

Or just issuing a

SELECT LAST_INSERT_ID()
directly to the DB. It's by connection, so you don't need to worry about race conditions. But be careful about multi-valued insert statements and INSERT INGOREs, since they behave "as documented" but strangely nonetheless.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users