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'
5 Answers
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

- 4,965
- 3
- 25
- 29
-
2To 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
-
2When 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
-
1I 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
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

- 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
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.

- 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
-
1wow 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
You can get all the info about logged user with global $user
variable.
The code is:
<?php
global $user;
$uid = $user->uid;
echo $uid;
?>
-
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
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.

- 33,075
- 15
- 67
- 82