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

user object

the user object has what you need...

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

-- andy

different users

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?

user_load

<?php
$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.

thanks

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.

just adding: as always, you

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:

<?php
  $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.

Maybe a bit simpler

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

simpler and faster

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.

login as that user

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

Arancaytar: <?php$user=user_l

Arancaytar:

<?php
$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:

<?php
  $another_user
= user_load($uid);
 
$username= $another_user->name;
?>

neyvaz, you are right: This

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.

This is great for other

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));
?>
nobody click here