User Contributed Notes: mysql_insert_id
sleibowitz@btcwcu.org
13-May-1999 12:25
This function also works for REPLACE operations (tested under 3.0.7). I would asume that it also works for UPDATEs, although I haven't tested it.
filip@yellowquest.com
15-Jun-1999 06:46
I have tested it with UPDATE under 3.0.7 and it gave me value '0' all the time ... it does not work but could be handy for the future
7518-204@online.de
29-Aug-1999 07:15
I have made
$an_id=mysql_query("INSERT (...)");
$lastid=mysql_insert_id();
echo $lastid;
I have tested the Query statement in the commandline interpretor and it works. The Entry is done by the PHP script, but the mysql_insert_id() gives back '0' !!!
WHAT WENT WRONG ??
esmith@schicktech.com
21-Sep-1999 12:37
I used mysql_insert_id with the $result from the query instead of the link id. Do both work, or just one?
avery@ikomm.com
20-Nov-1999 12:33
Does anyone know if mysql_insert_id() returns the last id inserted by the current thread or by any thread? I see a slim chance for problems if it returns the last id inserted by any thread, since another thread could do an insert in between the current thread's insert query and mysql_insert_id(). My guess is that it's just the id from the current thread but I don't know that's the case.
alvar.freude@merz-akademie.de
27-Nov-1999 02:01
After a delayed insert ("INSERT DELAYED ...") statement you get an last insert id with the value 0.
It only works with non delayert inserts!
vksgeneric@hotmail.com
09-Dec-1999 05:14
This is related to the three postings above this one:<br>
You can't do an INSERT DELAYED and expect to get anything but zero, for it runs in a separate thread, and mysql_insert_id() is tied to the current thread.
Vlad
karacupa@iname.com
28-Dec-1999 07:48
If you try call mysql_insert_id() after
"unlock tables" it'll return 0. So, only use it before.
jearom.at.hotmail.com
26-Jan-2000 03:30
If you're trying to use this function to determine the value of a field so that you can use it in an INSERT statement, don't. Rather, use NULL for the value of the auto-incremented field. This will work even if there are no records in the table you're inserting to, so long as the desired field has the AUTO_INCREMENT attribute.
arni@linux.is
26-Jan-2000 08:12
I noticed that when using MYSQL_QUERY() as a link identifier for MYSQL_INSERT_ID(), you will get a error message like this:
Warning: Supplied argument is not a valid MySQL-Link resource in...
I noticed that some of my PHP programs stopped working when I upgraded to PHP4beta.
The wrong way:
$query = mysql_query("insert...");
$id = mysql_insert_id($query);
The right way:
$query = mysql_query("insert...");
$id = mysql_insert_id();
$query is not a valid link identifier, it's value can only be 0 or 1. As said in the manual: "If link_identifier isn't specified, the last opened link is assumed.".
hans@girafe.net
23-Mar-2000 04:09
you have to use a link identifier,
$link=mysql_connect(....);
...
$newID= mysql_insert_id( $link );