Is there any way to get uid from user name in Drupal?

15,716

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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

The code is:

<?php
global $user;
$uid = $user->uid;
echo $uid;
?>
Share:
15,716
Nick.h
Author by

Nick.h

Updated on July 22, 2022

Comments

  • Nick.h
    Nick.h almost 2 years

    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'

  • googletorp
    googletorp over 13 years
    To get the uid, this is quite a slow way to do it, as it involves all modules that implement hook_user creating an unknown amount of queries.
  • Sid Kshatriya
    Sid Kshatriya over 13 years
    @googletorp: Agree this is slow. But I thought it would be simpler for Hamid this way. Practically speaking node_load, which is similar in concept to user_load happens a lot in Drupal, if I'm not wrong, and yet doesn't adversely affect performance in most cases. So I thought it would be a good idea to recommend user_load
  • googletorp
    googletorp over 13 years
    When he himself asks if he should just do a db query, I'm sure he will be able to do it. It's not that complicated after all.
  • Henrik Opel
    Henrik Opel over 13 years
    +1 - this would be the correct API function to use. Concerning performance, user_load() does not use static caching like node_load() does, so if that call is going to be made often during a page cycle, it might be better to do a custom query.
  • Dr Deo
    Dr Deo over 12 years
    dont put email on stackoverflow in this format. You know you will be spamed
  • ErichBSchulz
    ErichBSchulz over 11 years
    wow that is very dangerous query given its taking input from a form... google "injection attack"
  • David Meister
    David Meister over 11 years
    I agree with Henrik. Don't do user_load() unless you're about to actually use more information than just the uid. It does not cache in D6. D7 on the other hand...
  • Roger
    Roger about 11 years
    Your answeer is quite good because it's simple and it can be applied inside a custom validation function for login form (where user global var is no set yet) Thank you a lot! +1
  • Roger
    Roger about 11 years
    Your solution is the best but when you need to do this inside a custom validation function for login form you have to use Willem de Jong solution's. +1
  • Druvision
    Druvision over 10 years
    @EricBSchulz, yes it's dangerous but this is the best performing solution, especially in case there are lots of users to display. I tried to use the other methods but in my case user_load caused me to logout... so I am editing the answer to put check_plain around the username string.
  • Joshua Kissoon
    Joshua Kissoon over 10 years
    @Druvision there is no need for check_plain in this query, since arguments passed as I did above is escaped by drupal before the query is made