db_last_insert_id

6 database.mysql-common.inc db_last_insert_id($table, $field)
6 database.pgsql.inc db_last_insert_id($table, $field)

Returns the last insert id. This function is thread safe.

Parameters

$table: The name of the table you inserted into.

$field: The name of the autoincrement field.

Related topics

11 calls to db_last_insert_id()

File

includes/database.pgsql.inc, line 230
Database interface code for PostgreSQL database servers.

Code

function db_last_insert_id($table, $field) {
  return db_result(db_query("SELECT CURRVAL('{" . db_escape_table($table) . "}_" . db_escape_table($field) . "_seq')"));
}

Comments

Parameters only work with PostgreSQL

Be careful with the parameters, since they don't have any effect on MySQL.

This function does not work as expected - please read!

If you are looking at this function to return an id from a table specified in the parameters, please note that this does not work. All this function does is return the very last id inserted into the database, regardless of what table that id is from. You cannot specify which table you would like the id to be from, the parameters are only there because PostgreSQL needs them and nothing more. See here for more detail: http://drupal.org/node/369520

It worked for me....... It

It worked for me.......

It works when we check latest inserted id, right after our insertion query.... just like this

db_query("INSERT INTO {table_name}(name) VALUES ('myname')");
echo db_last_insert_id('table_name', 'name');

yes you'r write. work for me too......

You have to write the query in the following format

db_query("INSERT INTO {table_name}(name) VALUES ('myname')");
echo db_last_insert_id('table_name', 'name');

If you use SET column1 = value, column2 = value......
db_last_insert_id() will not work.

Thanks jamiehollern

jamiehollern is correct
db_last_insert_id is give me the last inserted row primary key value.
But when its not inserting in the given table its returning me watch dog table last inserted id but i am not retrieving watch dog incremented id..

Login or register to post comments