8.2.x user.module user_load($uid, $reset = FALSE)
8.0.x user.module user_load($uid, $reset = FALSE)
8.1.x user.module user_load($uid, $reset = FALSE)
8.3.x user.module user_load($uid, $reset = FALSE)
4.6.x user.module user_load($array = array())
4.7.x user.module user_load($array = array())
5.x user.module user_load($array = array())
6.x user.module user_load($user_info = array())
7.x user.module user_load($uid, $reset = FALSE)

Loads a user object.

Drupal has a global $user object, which represents the currently-logged-in user. So to avoid confusion and to avoid clobbering the global $user object, it is a good idea to assign the result of this function to a different local variable, generally $account. If you actually do want to act as the user you are loading, it is essential to call drupal_save_session(FALSE); first. See Safely impersonating another user for more information.

Parameters

$uid: Integer specifying the user ID to load.

$reset: TRUE to reset the internal cache and load from the database; FALSE (default) to load from the internal cache, if set.

Return value

A fully-loaded user object upon successful user load, or FALSE if the user cannot be loaded.

See also

user_load_multiple()

54 calls to user_load()
comment_tokens in modules/comment/comment.tokens.inc
Implements hook_tokens().
DrupalRenderTestCase::testDrupalRenderCache in modules/simpletest/tests/common.test
Tests caching of render items.
DrupalWebTestCase::setUp in modules/simpletest/drupal_web_test_case.php
Sets up a Drupal site for running functional and integration tests.
EntityCrudHookTestCase::testUserHooks in modules/simpletest/tests/entity_crud_hook_test.test
Tests hook invocations for CRUD operations on users.
FilterFormatUpgradePathTestCase::testFilterFormatUpgrade in modules/simpletest/tests/upgrade/upgrade.filter.test
Test a successful upgrade.

... See full list

File

modules/user/user.module, line 365
Enables the user registration and login system.

Code

function user_load($uid, $reset = FALSE) {
  $users = user_load_multiple(array($uid), array(), $reset);
  return reset($users);
}

Comments

tax14’s picture

If you have defined custom fields for the user, e.g. firstname, lastname, phone, address, city, etc. and want to get their values, e.g. to display the full username in a block, use $user_fields. For example,

global $user;
$user_fields = user_load($user->uid);

$firstname = $user_fields->field_firstname['und']['0']['value'];
$lastname = $user_fields->field_lastname['und']['0']['value'];

print  t("Welcome: " . $firstname. ' ' . $lastname) ; 
007pig’s picture

I think the recommend way is to use field_get_items() function instead of get the value by array.

See also:
http://drupal7ish.blogspot.com/2011/02/proper-way-to-get-field-values.html

adaddinsane’s picture

Oooh, that's my Drupal 7 blog - there's a better blog entry now with a little function to help you, here:

http://drupal7ish.blogspot.com/2011/03/getting-field-data-out-of-entitie...

adaddinsane’s picture

And now there's a module with a couple of very useful functions for extracting field data from entities:

http://j.mp/piHpfH

Fly my pretties...

Razeel Akbar’s picture

Instead of

global $user;
$user_fields = user_load($user->uid);

it's better to use attached $account object with username preprocess or $user object with nodes, in order to get the relevant $uid....

$user_feilds = user_load($variables['account']->uid);

And next best-est way as already mentioned...

field_get_items($entity_type, $entity, $field_name, $langcode = NULL)
grantlucas’s picture

If you're using user_load to load a user object, make sure that you don't assign the results to a variable name "$user" just incase you included the global $user variable at some point in that function. You will get some pretty unexpected results as you are overwriting who is currently logged in.

I recommend using something like $account instead.

Bevan’s picture

user_load('') and user_load(array(0 => '')) return a fully-entity-loaded anonymous user object. That is silly: https://drupal.org/node/2191491

kenorb’s picture

To load by name, try:

$users = user_load_multiple(array(), array('name' => $username, 'status' => '1'));
$account = reset($users);
ilclaudio’s picture

I have a memory leak problem with a script that uses user_load in a cycle in this line:
$account = user_load($userUid);
Could it be that loading many users with user_load in a cycle causes a memory leak?
Is there a way to "unload" a user?

cld