Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'

like image 626
Nick.h Avatar asked Oct 04 '10 15:10

Nick.h


2 Answers

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

like image 77
Willem de Jong Avatar answered Nov 20 '22 00:11

Willem de Jong


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
like image 11
Sid Kshatriya Avatar answered Nov 19 '22 22:11

Sid Kshatriya