When node_load won't load, and the anonymous user has vanished

Use these two SQL commands to repair your Drupal 6 site to restore the anonymous user database record:

INSERT INTO users (name, mail) VALUES ('', '');
UPDATE users SET uid=0 WHERE name='';

The symptoms you'll see that tip you off that this is needed:

  1. node_load() is not loading nodes that you can prove are in the node table.
  2. node_delete() is not deleting nodes that you can prove are in the node table.
  3. The query SELECT * FROM users WHERE uid=0; returns zero rows.

The reason you have to INSERT INTO and then UPDATE is because of the autoincrement functionality on the users table. If you don't believe me, try this experiment:

# delete user 0
mysql> DELETE FROM users WHERE uid=0;
Query OK, 1 row affected (0.00 sec)

# try to restore user 0 directly
mysql> INSERT INTO users (uid, name, mail) VALUES (0, '', '');
Query OK, 1 row affected (0.00 sec)

# see that it didn't work
mysql> SELECT uid FROM users WHERE name='';
+-------+
| uid   |
+-------+
| 19611 |
+-------+

Note that if you get a Duplicate entry error like the following, you need to delete the spurious rows prior to running the corrective queries I list at the beginning of the post.

# If you see this....
mysql> INSERT INTO users (name, mail) VALUES ('', '');
ERROR 1062 (23000): Duplicate entry '' for key 2

# Do this.
mysql> DELETE FROM users WHERE name='';

Comments

thanks for documenting this!

I had some VBO views that were returning 0 records even though I knew that there were nodes in the database that matched.

Low and behold...my other VBO for deleting users allowed me to delete UID 0 which caused the problem. And when it happened I knew to come here to find the queries to set me straight. Thanks, Rob!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.

More information about formatting options