db_next_id

  1. drupal
    1. 4.7
    2. 4.7 database.mysqli.inc
    3. 4.7 database.pgsql.inc
    4. 5 database.mysqli.inc
    5. 5
    6. 5 database.pgsql.inc
    7. 7 database.inc
Versions
4.7 – 5 db_next_id($name)
7 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.

Related topics

Code

includes/database.mysql.inc, line 206

<?php
function db_next_id($name) {
  $name = db_prefix_tables($name);
  db_query('LOCK TABLES {sequences} WRITE');
  $id = db_result(db_query("SELECT id FROM {sequences} WHERE name = '%s'", $name)) + 1;
  db_query("REPLACE INTO {sequences} VALUES ('%s', %d)", $name, $id);
  db_query('UNLOCK TABLES');

  return $id;
}
?>