[drupal-support] How to use MySQL last_insert_id function withlocking?

Nicolas Tostin nicolast at logis.com.mx
Thu Jul 21 14:06:33 UTC 2005



 KZ Mai wrote:
> Hi,
> I am developing my own module which uses tables that have auto-increment
fields.
>
> As far as I can tell, drupal uses a single connection for my default
> configuration.
>
> I don't think i think use last_insert_id without locking because the
> MySQL DB connection is shared accross requests.
>
> so what should I lock in order to get the correct result from
last_insert_id?

Well I'm not sure it's a good idea using last_insert_id, because such
function won't work if users of your module use pgSQL so, I think you should
try to keep compatibility between DB.

For this Drupal use db_next_id function that is preferable to mysql
auto_incrment fields.
So using this you get the next ID for your table and then you can make the
INSERT.

Nicolas




More information about the drupal-support mailing list