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

Nick.h picture Nick.h · Oct 4, 2010 · Viewed 15.2k times · Source

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'

Answer

Sid Kshatriya picture Sid Kshatriya · Oct 5, 2010

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