RE: mysql_insert_id() vs. last_insert_id()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> The one thing that jumps out at me is that if you insert multiple
> rows at the same time with an insert statement, 
> LAST_INSERT_ID() returns the
> value for the first inserted row only, whereas 
> MYSQL_INSERT_ID() returns the
> last id inserted. Am I reading that right?

Yeah, that's the way I interpreted it and I had the same question as you.
Why would you want to do that?

The only think I could think of is if your first INSERT statement inserted a
parent with an ID and the next, say, 5 inserts inserted children, if you
used LAST_INSERT_ID() you could get the ID of the parent.

I might be thinking this through the wrong way, but it IS on a
per-connection basis and if the first query ON THAT CONNECTION was a parent
and the rest were children I guess that scenario would hold true. I'm sure
there are many other examples of how this might be beneficial.

Any further thoughts?

Rich

> -----Original Message-----
> From: Randy Phillips [mailto:randy@phillips-it.com]
> Sent: Wednesday, January 29, 2003 9:30 AM
> To: php-db@lists.php.net
> Subject: Re:  mysql_insert_id() vs. last_insert_id()
> 
> 
> on 1/28/03 11:42 AM, John W. Holmes at holmes072000@charter.net wrote:
> 
> >> The reason I ask is, if you use mysql_insert_id() on a 
> busy server and
> > it
> >> does not function on a per-connection basis, don't you run 
> the risk of
> >> getting the last ID of somebody else's INSERT query on the server?
> > 
> > Yes, they are both on a per-connection basis. They'd be useless
> > otherwise.
> 
> 
> I agree. Although, the MySQL docs are confusing on the 
> distinction between
> these two functions in my opinion.
> 
> The one thing that jumps out at me is that if you insert multiple
> rows at the same time with an insert statement, 
> LAST_INSERT_ID() returns the
> value for the first inserted row only, whereas 
> MYSQL_INSERT_ID() returns the
> last id inserted. Am I reading that right?
> 
> I guess the question is: Why is it useful to only store the 
> first id? When
> would you want that?
> 
> 
> ----------------------------------------------------------------------
> Randy Phillips                                     www.phillips-it.com
> phillipsIT, L.L.C.                                 voice: 636.390.9468
> Senior Web Developer                                eFax: 501.423.3817
> ----------------------------------------------------------------------
>    Get SideKick -> for MySQL -> http://www.phillips-it.com/sidekick
> 
> 
> -- 
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux