I’ve been really enjoying working with Drupal - the more I learn about it, the more ideas I get on how to use it. Yet, sometimes it can make a simple thing a bit daunting - like fetching a user’s name from a user id, without needed to load the entire user object with user_load() - which can get expensive.
So, here’s a handy little function. Just plop it in your template.php file for your theme of choice, and it will be available where you need it on your site (as long as you’re using that template!):
function getUserName($id) {
$sql = "SELECT name FROM {users} WHERE uid = '$id'";
$result = db_query(db_rewrite_sql($sql));
$username = db_fetch_object($result);
return $username->name;
}
// after learning more Drupal, this is the better way:
// note that $id must be a number
function getUserName($id) {
return db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $id));
}
To use it for output, just do something like:
<?php print getUserName(1234); ?>
Using This With the Views Module
I often use this with the Views module. For example, I have a View for displaying all content created by a given user. The URL looks something like this:
http://www.some-drupal-site.com/user/1234/recent
The “1234″ is the user id argument getting passed to the view. The view will then go in and fetch content created by this user id. Oftentimes I want to create a nice header in the View with dynamic output - but Views doesn’t really allow that sort of thing in the header/footer areas under Basic Settings. So I use this function to get around it. In the view’s header, I do this to fetch a username:
<?php $view=views_get_current_view(); $user_name = getUserName($view->args[0]); print '<h1>Content created by ' . $user_name . '</h1>'; ?>
*Note that to get the PHP input type working, you need to allow the PHP Filter in the modules section, and then select PHP as the input type for this view’s header.
Note that after you click the Update button, the Views module may give you an error message - that’s OK. It just doesn’t know what to do with this php code outside of the actual page you’re going to use it on.Below is a screenshot example.

Hope this helps someone! And if you know a better way to do this, speak up! Cheers.
Thanks for the great help. But for some reason this only works for logged users on my site. Both in the normal page and a header created by views. Any idea why this wouldn’t work. I looked at the perms. but they seem fine.
Thanks,
mark
Hi Mark,
If it’s not working on a normal page, then fixing it here will likely fix the Views part too. What exactly are you passing into the function? I would start debugging by passing in the number 1 (as an integer, not a string like “1″) and seeing if that works. 1 is the super user, so you know that user exists - maybe you know that ;)
OMG enjoyed reading your blogpost. I added your rss to my reader!
Hey all - author Matt here. Just realized I didn’t code up the most “Drupal” way to do this, so I updated the function above, which takes advantage of Drupal’s SQL-sanitation. Plus, it’s only 1 line now - cheers!
How can do I apply this when there are multiple $id values? What is the php for the array?
@Katie - if you have an array of user ids, you’ll probably want to use something like a foreach loop, and apply the above function to every value while you’re looping through the array.
http://php.net/manual/en/control-structures.foreach.php
Arrays and “looping” is a very handy thing to learn ;)
@Matt:
Thanks for the tip. I get the array to print, but it only shows the first value in the embedded view. Do you happen to know how to fix my code (I did check that views allows for unlimited results for the block)
foreach ($node->field_email as $i) {
$user_id = getUserName($i['value']);
print $user_id;}
print views_embed_view(’user_refer’, ‘block_1′, $user_id);
Hi Katie,
I think you’re having some general problems understanding a foreach loop and what views_embed_views does. If I were you, I’d take this over the Drupal forum, and post a description of what you’re trying to do with some code examples, since we’re getting off-topic here. My hope is that with some help, you’ll be able to do the entire thing within Views instead of writing code. Feel free to post a link to that back here and I’ll do my best to take a look.