Community Documentation

db_next_id

5 database.mysql.inc db_next_id($name)
5 database.pgsql.inc db_next_id($name)
5 database.mysqli.inc db_next_id($name)
7 database.inc db_next_id($existing_id = 0)
8 database.inc db_next_id($existing_id = 0)

Return a new unique ID in the given sequence.

For compatibility reasons, Drupal does not use auto-numbered fields in its database tables. Instead, this function is used to return a new unique ID of the type requested. If necessary, a new sequence with the given name will be created.

Note that the table name should be in curly brackets to preserve compatibility with table prefixes. For example, db_next_id('{node}_nid');

Related topics

▾ 14 functions call db_next_id()

aggregator_save_category in modules/aggregator/aggregator.module
Add/edit/delete aggregator categories.
aggregator_save_feed in modules/aggregator/aggregator.module
Add/edit/delete an aggregator feed.
aggregator_save_item in modules/aggregator/aggregator.module
comment_save in modules/comment/comment.module
Accepts a submission of new or changed comment content.
drupal_client_ping in modules/drupal/drupal.module
Callback function from drupal_xmlrpc() called when another site pings this one.
menu_save_item in modules/menu/menu.module
Save a menu item to the database.
node_save in modules/node/node.module
Save a node object into the database.
system_update_146 in modules/system/system.install
system_update_151 in modules/system/system.install
system_update_159 in modules/system/system.install
Retrieve data out of the old_revisions table and put into new revision system.
taxonomy_save_term in modules/taxonomy/taxonomy.module
Helper function for taxonomy_form_term_submit().
taxonomy_save_vocabulary in modules/taxonomy/taxonomy.module
upload_save in modules/upload/upload.module
user_save in modules/user/user.module
Save changes to a user account or add a new user.

File

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

Code

<?php
function db_next_id($name) {
  $id = db_result(db_query("SELECT nextval('%s_seq')", db_prefix_tables($name)));
  return $id;
}
?>

Comments

So this is not available at

So this is not available at all in version 6?

The sequence table is

The sequence table is deprecated in Drupal 6 - I believe.

New serial ids are returned by drupal_write_record in 6.x

Getting the ids for serial fields is done through drupal_write_record() in 6.x

The new ids are returned by the write call - see the drupal_write_record help for version 6 ( http://api.drupal.org/api/function/drupal_write_record/6 ) for more information.

In Drupal 7 the purpose of this call is different to 4/5, according to the notes, it's intended for cases where you can't use a serial field at all.

drupal 6

for drupal 6 you can use db_last_insert_id and add 1.

Login or register to post comments