The documentation implies that the function db_last_insert_id() can be used to return an id from a table specified in the parameters, however all this function does is return the very last id inserted into the database, regardless of what table that id is from. The documentation could be clarified so that users are aware of two points:
1) The parameters are there only for compatibility with PostgreSQL
2) Regardless of the type of database, the function will only return the very last id inserted into the database and will not return an id from a table specified in the parameters

Comments

jhodgdon’s picture

Title: Documentation problem with db_last_insert_id » db_last_insert_id needs to make clear that it's just the last insert, not table/field specific

OK, I agree that this could be clearer on the MySQL version of this function:
http://api.drupal.org/api/drupal/includes--database.mysql-common.inc/fun...

I'm not clear on whether the statement you are making is true for the PostgreSQL version though:
http://api.drupal.org/api/drupal/includes--database.pgsql.inc/function/d...

In any case, the next step would be to make a patch.

jhodgdon’s picture

Issue summary: View changes
Status: Active » Closed (won't fix)

I am cleaning up old 6.x documentation issues. At this point, we are not spending effort fixing them. Sorry.

mattumd’s picture

I have done many works with db_last_insert_id and as far as I recall, I never had a case where I could or even wanted to get the ID of table. If by ID of table, you are referring to table name, I am sure it is not possible.

Another scenario that I run into was to get db_last_insert_id from a table whose connection and query was cross-database. So in those cases, you have to make sure to add database name in your query.

Another complex scenario is to use it in a chain of queries inside a MySQL transaction.

So even if PostgreSQL allows return of table ID, you have to be careful on how to use in cross-database queries since there is a chance two tables from two different databases have similar name or ID.

Thanks,

Matt
https://coding-bootcamps.com/
https://myhsts.org/
https://dcwebmakers.com/