I need to get the last inserted id in drupal 7. I am using

drupal_write_record('user_log', $log)

How we can get the last_instered_id?

2 Answers 2


Assuming your {user_log} table has a PRIMARY KEY with it, or something else serial, then it should be set in in the array you passed to it. In your case, it would be something like

$last_log_id = $log['log_id'];

If you are not dependent on drupal_write_record you can use db_insert and/or db_update instead. The execute() method of the query objects returns the current id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.