Answers:
这很正常,因为Drupal在安装时会为匿名用户创建该条目。这是通过user_install()(Drupal 7)或system_install()完成的,其中包含以下代码。
// 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'", '');
当将“节点”表中包含的数据与“用户”表中包含的数据连接在一起时,通常使用该条目。
没有该条目将导致Drupal在某些情况下无法正常工作。
如果您需要恢复数据库中的匿名用户数据,我将执行类似于从Drupal执行的代码。特别是对于Drupal 6,我将执行以下代码。
如果数据库中已经存在匿名用户的数据,但用户ID不为0:
db_query("UPDATE {users} SET uid = uid - uid WHERE name = '%s'", '');
如果匿名用户的数据不存在,即使使用错误的用户ID:
db_query("INSERT INTO {users} (name, mail) VALUES('%s', '%s')", '', '');
db_query("UPDATE {users} SET uid = uid - uid WHERE name = '%s'", '');
如果要自动还原匿名用户数据,则可以hook_cron()
在自定义模块中实现,并执行类似于以下代码的代码。(该代码用于Drupal6。)
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'", '');
}
如果您赋予模块较低的权重,则其实hook_cron()
现将在其他实现之前执行,这样可以避免它们由于数据库中缺少行而失败。
INSERT INTO users (uid, name, mail) VALUES(0, '', '')