Drupal中有没有办法从用户名获取uid?

发布于 2024-09-26 04:22:42 字数 102 浏览 5 评论 0原文

Drupal中有没有从用户名获取uid的核心功能? 或者我应该执行数据库查询? 我的字段是一个文本字段,其中“#autocomplete_path”等于“user/autocomplete”

Is there any core function to get uid from username in Drupal?
Or I should perform a db query?
my field is a textfield with '#autocomplete_path' equal to 'user/autocomplete'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

初懵 2024-10-03 04:22:42

您可以使用user_load函数。请参阅http://api.drupal.org/api/function/user_load/6

特别参见 http://api.drupal.org/api/function /user_load/6#comment-6439

所以你会做这样的事情:

// $name is the user name
$account = user_load(array('name' => check_plain($name)));
// uid is now available as $account->uid

You can use the user_load function. See http://api.drupal.org/api/function/user_load/6

In particular see http://api.drupal.org/api/function/user_load/6#comment-6439

So you would do something like this:

// $name is the user name
$account = user_load(array('name' => check_plain($name)));
// uid is now available as $account->uid
末骤雨初歇 2024-10-03 04:22:42

不知怎的,我无法使查询工作,但我发现了这个:

$user = user_load_by_name($username);
$user_id = $user->uid;

请参阅: http://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_load_by_name/7

Somehow I couldn't make the query work but I found this:

$user = user_load_by_name($username);
$user_id = $user->uid;

see: http://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_load_by_name/7

想你的星星会说话 2024-10-03 04:22:42

用户加载函数非常繁重,会消耗更多资源并返回比所需更多的数据,这里有一个不错的小函数供您使用:

function get_uid($username)
{    
    // Function that returns the uid based on the username given
    $user = db_fetch_object(db_query("SELECT uid FROM users WHERE name=':username'", array(":username" => $username)));

    return $user->uid;
}

注意:此代码经过修改并且输入被转义,因此该代码没有任何危险。

The user load function is very heavy, would use up more resources and return more data than required, Here is a nice little function for you:

function get_uid($username)
{    
    // Function that returns the uid based on the username given
    $user = db_fetch_object(db_query("SELECT uid FROM users WHERE name=':username'", array(":username" => $username)));

    return $user->uid;
}

Note: This code is revised and input is escaped, so the code is not dangerous in any way.

冰魂雪魄 2024-10-03 04:22:42

您可以使用全局 $user 变量获取有关登录用户的所有信息。

代码是:

<?php
global $user;
$uid = $user->uid;
echo $uid;
?>

You can get all the info about logged user with global $user variable.

The code is:

<?php
global $user;
$uid = $user->uid;
echo $uid;
?>
你不是我要的菜∠ 2024-10-03 04:22:42

据我所知,目前还没有现成的 API 函数可以实现这一点。但如果您需要多个地方,您可以自己制作。查询数据库中的 uid 应该非常简单直接。

There is no ready made API function for this, not that I know of. But you can make your own if you needs several places. It should be pretty simple and straight forward to query the db for the uid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文