Click to See Complete Forum and Search --> : [RESOLVED] INSERT + SELECT simultaneously


Ultimater
07-21-2006, 02:40 PM
My table has an auto_increment field and I'd like to be able to INSERT into the table and return the value assigned to the auto_increment back to PHP via a mysql_query.

INSERTs generally don't return anything back to the query relative to the entry, to the best of my knowledge.

I've considered using mysql_query("SELECT UUID()") to generate a Universal Unique Identifier and use the UUID as the primary key instead of the auto_increment however I was wondering if there were some way to query an INSERT which would return a reference to the row added so I wouldn't need to resort to UUIDs....

I've considered using mysql_insert_id() however I'm not so sure how safe it is.

php.net gives the following example:

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());

which is followed by some notes of caution:

Note: Because mysql_insert_id() acts on the last performed query,
be sure to call mysql_insert_id() immediately after the query that generates the value.

Note: The value of the MySQL SQL function LAST_INSERT_ID() always
contains the most recently generated AUTO_INCREMENT value, and is not reset between queries.


Is there a way to actually reference the row added to the table through the INSERT query rather than pulling-up the last entry added to the database which could have been added from another process?

mattyblah
07-21-2006, 04:23 PM
I believe mysql_insert_id is connection safe.

chazzy
07-21-2006, 04:43 PM
I believe mysql_insert_id is connection safe.
Agreed.

Ultimater
07-21-2006, 06:06 PM
Thanks guys!