Jump to content


Returning row cardinality after insert


4 replies to this topic

#1 Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 05 May 2006 - 05:43 PM

Hey,

I know rc69 is really good with this. I hope he's got an answer.

Anyways, so I have a table built like such

ID | Info

1 Blah
2 Poo!


So, I'm going to insert another row

INSERT INTO table (info) VALUES ('w00t')

Now, what I'd like to know how to return the inserted row ID for the ID field. So the next should be 3 but I want to return this number to me. But it may not be 3 in some cases (if I deleted a row) so it could be 4. I just want it to return the inserted row ID.


Hope I explained it well,
Thanks in advance

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 06 May 2006 - 01:09 AM

If you wanted my help specifically you coulda pm'd me :P

http://dev.mysql.com/doc/refman/4.1/en/get...-unique-id.html
I'm out of time right now, and out for the weekend, so the only other advice i can give is to do a forum search, this has been asked before i believe.

#3 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 06 May 2006 - 02:40 AM

well, one thing to note is that if the id is your primary key, as most tables should have, you don't want the id's reused after they've been deleted. it's bad database design. it's better to just let the database auto-assign the primary key ID so that each entry gets its own unique ID.

#4 liveman

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Location:New Jersey

Posted 06 May 2006 - 12:38 PM

View PostSpaceGhost, on May 6 2006, 03:40 AM, said:

well, one thing to note is that if the id is your primary key, as most tables should have, you don't want the id's reused after they've been deleted. it's bad database design. it's better to just let the database auto-assign the primary key ID so that each entry gets its own unique ID.

He knows that, and he has that but if you delete a row for some un knownreason the autu-assign won't go back to the missing ID anyhow http://us2.php.net/m...l-insert-id.php is what you are looking for

#5 Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 07 May 2006 - 04:56 PM

Thanks liveman. Yes I had a friend point out the function and I slapped myself several times.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users