★ wanayoo — archive 1999 http://www.php.net/manual/function.mysql-insert-id.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to MySQL
Quick Reference
MySQL
* mysql_affected_rows
* mysql_change_user
* mysql_close
* mysql_connect
* mysql_create_db
* mysql_data_seek
* mysql_db_query
* mysql_drop_db
* mysql_errno
* mysql_error
* mysql_fetch_array
* mysql_fetch_field
* mysql_fetch_lengths
* mysql_fetch_object
* mysql_fetch_row
* mysql_field_name
* mysql_field_seek
* mysql_field_table
* mysql_field_type
* mysql_field_flags
* mysql_field_len
* mysql_free_result
* mysql_insert_id
* mysql_list_fields
* mysql_list_dbs
* mysql_list_tables
* mysql_num_fields
* mysql_num_rows
* mysql_pconnect
* mysql_query
* mysql_result
* mysql_select_db
* mysql_tablename
Manual: mysql_insert_id
View the source code for this pageSearch the site



Previous page
 mysql_free_result
mysql_list_fields 
Next page


mysql_insert_id

mysql_insert_id -- Get the id generated from the previous INSERT operation

Description

int mysql_insert_id(int [link_identifier] );

mysql_insert_id() returns the ID generated for an AUTO_INCREMENTED field. It will return the auto-generated ID returned by the last INSERT query performed using the given link_identifier. If link_identifier isn't specified, the last opened link is assumed.


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 );



 About Notes


Previous page
 mysql_free_result
mysql_list_fields 
Next page



Site Statistics


Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.