Is there a function in the core which returns a username based on any given user id?

something like $username = get_username($user_id)?

It would be easy enough to write my own, but I'm guessing one already exists -- any idea?

Thanks

Comments

arh1’s picture

the user object has what you need...

global $user;
print_r($user);
$username = $user->name;

-- andy

baker13c’s picture

thanks for the quick response, andy. appreciate it. im looking more to obtain different usernames based on a supplied user id, rather then the username of whoever is logged in. so say I'm logged in with user id #1 and i need to find the username of user id #12. Im wondering what the function might be to obtain the username of #12. its gotta be something basic like $username = get_username(12). any suggestions?

cburschka’s picture

$user=user_load($uid);
$username=$user->name;

(I think that's correct anyway)

Note that if you are working on a node whose author you want to display (happens sometimes), the author's name will already be loaded in $node->name.

baker13c’s picture

thanks for the help! instead of loading the whole user object for each given user, i decided just to pull each username directly from the Database:

$sql = "SELECT name FROM {users} WHERE uid = '$data->uid'";
$result = db_query(db_rewrite_sql($sql));
$username = db_fetch_object($result);
$username = $username->name;

thats the best i could do using drupal's sql commands. seems to be working fine.

thanks again for the help.

neyvaz’s picture

just adding:

as always, you really should not put variables into sql queries directly.
(You never really know what one day someone decides to put into that variable. This is a security issue.)

Instead, simply put a placeholder into the sql query.
And add your variable to the list of db_query() variables like that:

  $sql = "SELECT name FROM {users} WHERE uid = '%d'";
  $result = db_query(db_rewrite_sql($sql), $data->uid);
  $username = db_fetch_object($result);
  $username = $username->name;

Drupal will take care of funny and dangerous stuff for you, this way.

Ralla’s picture

  $username = db_result(db_query("SELECT name FROM {users} WHERE uid = '%s'", $data->uid));
SQP’s picture

made some tests on my server to compare each solution (results are in second and for 10000 calls)

user load : 6.256
dbtng db_select : 4.412
db_query db_fetch_object : 1.692
db_query db_result : 1.289

functions used were :

$name = user_load($data->uid)->name;

$name = db_select('users', 'u')->fields('u',array('name'))->condition('uid', $data->uid)->execute()->fetchField();

$result = db_query(db_rewrite_sql("SELECT name FROM {users} WHERE uid = '%d'"), $data->uid);
$name = db_fetch_object($result)->name;

$name = db_result(db_query("SELECT name FROM {users} WHERE uid = '%s'", $data->uid));

The last function wins only by little if we add the db_rewrite_sql, but it shouldn't be required as is says it's only for node, taxonomy and comment queries.
Guess I'll stick with the last one, but the dbtng code wasn't bad either.

Whackler’s picture

When i try this from Arancaytar, it will log me in as that user....

neyvaz’s picture

Arancaytar:

$user=user_load($uid);
$username=$user->name;

When using $user as temporary storage (that specific variable name), you are (might be) actually overwriting the $user object of the currently logged in user.
Simply use a different variable name. Like $another_user:

  $another_user= user_load($uid);
  $username= $another_user->name;
emartos’s picture

neyvaz, you are right: This code has a security bug. When you override $user variable, you could be giving access to your backend to anonymous visitors.

It's always better to use another name, such as $testUser or something similar.

dmetzcher’s picture

This is great for other little things, like getting a path to create a link (if using PathAuto to generate paths with the username in them). So you could use this, I suppose, to get the username, do a little conversion on the username to replace spaces with dashes (and change it to lowercase, if you want), and then print it with the rest of the path. That exactly what I'm looking to do, in fact. :-)

Handy little bit of code. Thanks for posting it for us!

Here's what I'm using it for...
Given a username of "John Smith", the following code will convert it to "john-smith". Just a simple bit of code, but VERY helpful when trying to get the proper path that the PathAuto module has created for a user's account page or blog.

<?php
$user=user_load(arg(1));
$username=$user->name;
print strtolower(preg_replace('/[^a-zA-Z0-9\-]/si' , '-' , $username));
?>
dgtlmoon’s picture

Avoid using "$user" as the variable name when using user_load, you could accidently replace the current user with that loaded user, be in the habbit of using $account instead

alan d.’s picture

Needed to google search the function for d7, and this page came up. Oh well, code search.

$name = format_username($account);
$name = format_username($node);

Or by user id.

if ($account = user_load()) {
  $name = format_username($account);
}
else {
  $name = variable_get('anonymous', t('Anonymous'));
}

This uses the Name field or Real name modules to use something other than the $user->name property (security thing). If neither are installed, the $user->name is used.


Alan Davison