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

5

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'

Masakomasan answered 4/10, 2010 at 15:6 Comment(0)
A
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
Alina answered 5/10, 2010 at 6:36 Comment(5)
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.Leatherwood
@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_loadAlina
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.Leatherwood
+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.Whipstitch
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...Usage
K
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

Kelbee answered 5/9, 2012 at 10:55 Comment(1)
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! +1Putrescine
H
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.

Hyperdulia answered 17/6, 2011 at 5:44 Comment(4)
dont put email on stackoverflow in this format. You know you will be spamedRockingham
wow that is very dangerous query given its taking input from a form... google "injection attack"Emalia
@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.Capacious
@Capacious 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 madeHyperdulia
S
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;
?>
Saleswoman answered 26/1, 2013 at 5:3 Comment(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. +1Putrescine
L
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.

Leatherwood answered 5/10, 2010 at 6:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.