9

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'

Sid Kshatriya
  • 4,965
  • 3
  • 25
  • 29
Nick.h
  • 4,035
  • 5
  • 21
  • 22

5 Answers5

11

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
Sid Kshatriya
  • 4,965
  • 3
  • 25
  • 29
  • 2
    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. – googletorp Oct 05 '10 at 06:39
  • @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` – Sid Kshatriya Oct 05 '10 at 06:56
  • 2
    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. – googletorp Oct 05 '10 at 07:05
  • 4
    +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. – Henrik Opel Oct 05 '10 at 09:07
  • 1
    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... – David Meister Aug 26 '12 at 11:32
10

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

Willem de Jong
  • 905
  • 2
  • 15
  • 31
  • 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 Feb 04 '13 at 11:04
5

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.

Joshua Kissoon
  • 3,269
  • 6
  • 32
  • 58
  • dont put email on stackoverflow in this format. You know you will be spamed – Dr Deo Oct 02 '11 at 12:07
  • 1
    wow that is very dangerous query given its taking input from a form... google "injection attack" – ErichBSchulz Aug 16 '12 at 11:24
  • @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. – Druvision Nov 06 '13 at 12:43
  • @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 – Joshua Kissoon Nov 06 '13 at 17:22
2

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

The code is:

<?php
global $user;
$uid = $user->uid;
echo $uid;
?>
Daniel
  • 10,864
  • 22
  • 84
  • 115
Neeraj
  • 21
  • 1
  • 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 – Roger Feb 04 '13 at 11:06
0

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.

googletorp
  • 33,075
  • 15
  • 67
  • 82