[UPHPU] mySQL Primary Keys
David Boucha
boucha at gmail.com
Tue Jun 29 22:51:27 MDT 2004
LAST_INSERT_ID() and/or mysql_insert_id() will work for you.
According to the MySQL Manual they are only affected by the current
client connection. They won't be affected by other users.
If you're id is autoincremented you do your insert statement and then
something like:
$mynewID = LAST_INSERT_ID();
Try reading the following:
http://dev.mysql.com/doc/mysql/en/Getting_unique_ID.html
http://dev.mysql.com/doc/mysql/en/mysql_insert_id.html
Dave
On Tue, 29 Jun 2004 21:55:36 -0600, Jacob Hanson <jacdx at jacobhanson.com> wrote:
>
> Tuesday, June 29, 2004, 9:37:10 PM, you wrote:
> > Just after an inserted row, I often need to get the id number (the
> > primary key, usually an integer) of the newly created row. Is there any
> > really slick way to get this?
>
> > Right now I am just doing a SELECT using the data I just inserted and
> > getting the newly created Id number - or I do something like:
>
> > SELECT * FROM `table` ORDER BY `primary key` DESC LIMIT 0,1
>
> > Which gives me the newest created row, but I'm not sure that's the best
> > way to do it. If it matters any, I am using Pear::DB...
>
> > --J
>
> > Hello John,
>
> This uses Pear::DB's (more portable) sequences to retrieve the next
> value, which is more robust than MySQL's auto increment stuff.
>
> http://pear.php.net/manual/en/package.database.db.db-common.nextid.php
>
> Or the hacky thing to try would be mysql_insert_id($dbh). The
> query above and mysql_insert_id are prone to concurrency issues, in
> the slight chance that another user would insert a record right after
> you did, giving you the wrong id.
>
> See ya,
> --
> _/ _/ _/ Jacob Hanson
> _/ _/_/_/ mailto:jacdx at jacobhanson.com
> _/_/_/ _/ _/ http://www.jacobhanson.com
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: list-unsubscribe at uphpu.org
> For additional commands, e-mail: list-help at uphpu.org
>
>
More information about the UPHPU
mailing list