i havent used php, but this following would be my guess:

mysql_last_id() most likely uses @@identity internaly.  Someone mentioned
that it returns last insert id per connection, then I am guessing that it
calls select @@identity  immediatly after any insert, and stores it to be
returned later in the mysql_last_id() call.

If there are mass insertions going on, seems to me the only way to guarantee
the right id is to do it in steps:
Insert blank record with some field that is unique to you (like a session id
for example), and then do select ID (or max(ID)) where this field is equal
to your session id. this way you will get back your autonumber and then
update the record with valid information...

You can always have extra field in tables specifically for this session id,
this way you can insert right away without an update, but thats an extra
field that has no other use.



----- Original Message -----
From: "Jonas Geiregat" <[EMAIL PROTECTED]>
To: "Mysql" <[EMAIL PROTECTED]>
Sent: Wednesday, June 18, 2003 1:35 PM
Subject: Re: @@identity


> Don Read wrote:
>
> >On 18-Jun-2003 Jonas Geiregat wrote:
> >
> >
> >>I'm using php + mysql for my project
> >>I want to get the last insert ID.
> >>I could use the php function mysql_last_id();
> >>but I could aslo use @@identity.
> >>Now some people have advised me NOT to use @@identity, cause it's not
> >>save buggy sometimes slow ..
> >>is this true am I better of with the php function ?
> >>
> >>
> >
> >Don't use either one.
> >
> >PHP's mysql_last_id() is stored as a _long; it'll break on BIGINT ids.
> >
> >If you need to keep the value in a PHP variable, use
> >'SELECT LAST_INSERT_ID() as id', and retrive it as a string.
> >
> >
> >
> >
> >>also I do my query insert something
> >>after that query I do an other query to get the last insert ID,
> >>if someone else does a insert query between those 2 query's won't that
> >>affect my last insert ID and won't I get the wrong one ?
> >>
> >>
> >>
> >
> >No. The id is per connection.
> >The return value is the last insert performed by your connection.
> >
> >Regards,
> >
> >
>
> Why did they make select @@identity then ?
> Is it just cause most other db's have this thing ?
> and what's the difference between @@identity and select last_insert_id() ?
>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:
http://lists.mysql.com/[EMAIL PROTECTED]
>
>


-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to