Jump to content


Photo

mysql_insert_id() or last_insert_id() ?


  • Please log in to reply
3 replies to this topic

#1 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 13 October 2006 - 10:24 PM

Which one should I use?

I have a query that inserts some info into a table named nights, when that query is done I have another one that inserts info into a table named rTables, in rTables I also have to insert the id  of the last inserted row. If i I use mysql_insert_id() it might give me the id of another table if someone else is doing something on the site.

I was wondering if LAST_INSERT_ID() does the same thing.. and what would be the best way of getting the id of the last row added in a table

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 13 October 2006 - 10:42 PM

mysql_insert_id() returns the last value for the current db connection so is unaffected by anyone else.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 13 October 2006 - 10:43 PM

mysql_insert_id() is the one you want. It will only retrieve the last id generated for the corrent connection. LAST_INSERT_ID will retrieve the last (global) insert id.

#4 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 13 October 2006 - 11:00 PM

alright thanks guys




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users