Drupal: Get a Username from a User ID

Posted February 17th @ 5:02 pm  |  Filed in: Drupal    

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.

user-header-views.png

Hope this helps someone! And if you know a better way to do this, speak up! Cheers.

1 Trackbacks/Pingbacks

  1. Pingback: Drupal - Adding Author Name Search to Main Search Results Page | GiveGoodWeb on March 17, 2009

8 Comments

  1. » Mark Reilly said on September 28, 2009 at 12:11...

    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

  2. http://www.stoptimestudio.com » Matt said on September 28, 2009 at 13:20...

    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 ;)

  3. http://matthewsblog.shotblogs.com/ » DeeplyJexYses said on December 11, 2009 at 17:57...

    OMG enjoyed reading your blogpost. I added your rss to my reader!

  4. http://www.stoptimestudio.com » Matt said on December 14, 2009 at 17:09...

    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!

  5. » Katie said on January 27, 2010 at 17:45...

    How can do I apply this when there are multiple $id values? What is the php for the array?

  6. http://www.stoptimestudio.com » Matt said on January 28, 2010 at 08:21...

    @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 ;)

  7. » Katie said on January 28, 2010 at 22:19...

    @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);

  8. http://www.stoptimestudio.com » Matt said on January 29, 2010 at 12:51...

    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.

Leave a comment

OpenID Login

Standard Login

Options:

Size

Colors