Alex Rivera | Logout

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

Asked 2010-10-04T15:06:19.850
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'

Edit
Report

1 Answer

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.

answered 2011-06-17T05:44:00.147

Your Answer