[thelist] SELECTing and auto_increment field

Bill Haenel bill at webmarketingworx.com
Thu Sep 13 17:55:56 CDT 2001

> PHP's mysql_insert_id() returns the last auto_incremented value, or
> the auto_incremented value of a given result resource, which means I
> can let mysql set the timestamp automagically too.

I had a similar challenge recently, but for this: I needed to not only pick
up the id from the last INSERT operation, but maybe instead the last UPDATE,
depending on however a record was last edited.

I needed to display that id for the next user, who may not show up for a
while. The last record edited may not have been the last one created, so a
highest-id/sequential solution wouldn't work.

My solution was to use a separate, two-column table that contains the id of
the last record edited, and the name of the table it comes from. I update
that last_id field at the end of every INSERT or UPDATE. Then I select it
using the appropriate table name whenever I need to refer to it. Seems
convoluted, but it works.

Is there a simpler solution? I did rtfm, but didn't find anything.

More information about the thelist mailing list