Is there any way to get uid from user name in Drupal?
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;
?>
Nick.h
Updated on July 22, 2022Comments
-
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 over 13 yearsTo 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 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 touser_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 recommenduser_load
-
googletorp over 13 yearsWhen 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 over 13 years+1 - this would be the correct API function to use. Concerning performance,
user_load()
does not use static caching likenode_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 over 12 yearsdont put email on stackoverflow in this format. You know you will be spamed
-
ErichBSchulz over 11 yearswow that is very dangerous query given its taking input from a form... google "injection attack"
-
David Meister over 11 yearsI 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 about 11 yearsYour 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 about 11 yearsYour 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 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 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