9

Is it normal to have a user with uid of 0 in users table?

17

It is normal, as Drupal creates that entry when it is installed, for the anonymous user. That is done from user_install() (Drupal 7), or system_install(), which contain the following code.

  // Drupal 7.
  // Insert a row for the anonymous user.
  db_insert('users')
    ->fields(array(
    'uid' => 0, 
    'name' => '', 
    'mail' => '',
  ))
    ->execute();

  // Drupal 6.
  // Inserting uid 0 here confuses MySQL -- the next user might be created as
  // uid 2 which is not what we want. So we insert the first user here, the
  // anonymous user. uid is 1 here for now, but very soon it will be changed
  // to 0.
  db_query("INSERT INTO {users} (name, mail) VALUES('%s', '%s')", '', '');
  // …
  // This sets the above two users uid 0 (anonymous). We avoid an explicit 0
  // otherwise MySQL might insert the next auto_increment value.
  db_query("UPDATE {users} SET uid = uid - uid WHERE name = '%s'", '');  

That entry is normally used when joining the data contained in the "node" table with the data contained in the "users" table.

Not having that entry would cause Drupal not to work correctly in some circumstances.

If you need to restore the anonymous user data in the database, I would execute code similar to the one executed from Drupal. In particular, for Drupal 6, I would execute the following code.

  • If the data for the anonymous users already exists in the database, but the user ID is not 0:

    db_query("UPDATE {users} SET uid = uid - uid WHERE name = '%s'", '');
    
  • If the data for the anonymous user don't exist, even with the wrong user ID:

    db_query("INSERT INTO {users} (name, mail) VALUES('%s', '%s')", '', '');
    db_query("UPDATE {users} SET uid = uid - uid WHERE name = '%s'", '');
    

If you want to automatically restore the anonymous user data, you could implement hook_cron() in a custom module, and execute code similar to the following one. (The code is for Drupal 6.)

function mymodule_cron() {
  $uid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", ''));

  if ($uid === FALSE) {
    // The data has not been found in the database; re-create the row.
    db_query("INSERT INTO {users} (name, mail) VALUES('%s', '%s')", '', '');
  }

  db_query("UPDATE {users} SET uid = uid - uid WHERE name = '%s'", '');
}

If you give to the module a lower weight, its implementation of hook_cron() will be executed before the other implementations, and this would avoid they will fail because the missing row in the database.

  • I wasn't prepared for this twist... :| Was sitting around for hours thinking why some posts have it (I thought it was a bug in my dump initially and simply removed it :O). What are the circumstances? Any resource on that? – jayarjo Jun 27 '11 at 19:57
  • I expanded my answer. It is normally used when getting data about the authors of the nodes. – kiamlaluno Jun 27 '11 at 20:09
  • 1
    It also leads to nasty warnings when running cron and other instances. So you should really re-add that row. – Berdir Jun 27 '11 at 20:17
  • 3
    If you need to restore the anonymous user, running this SQL on the database should suffice: INSERT INTO users (uid, name, mail) VALUES(0, '', '') – marcvangend Jun 27 '11 at 21:19
  • I felt it being a hack of some kind, that's why I thought it was weird and removed it. But now I got a prove of this as I was exporting my database in MYSQL40 compatibility mode (some dumb shared hosting server), it got imported as next auto-increment value (7). If I hadn't accidentally stumbled upon this thing, I would never knew what went wrong and would have wondered about disappeared posts for indefinite amount of time :( Not right... – jayarjo Jun 28 '11 at 13:05
2

By default anonymous user is 0 and this is the first user present in the users table at the time of installing drupal and the admin id will be 1 and he will be the second user in the users table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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