I know I can get user information from any user with the getUser( $id ) object:

$user = JFactory::getUser();

if (!$user->guest) {
  echo 'You are logged in as:<br />';
  echo 'User name: ' . $user->username . '<br />';
  echo 'Real name: ' . $user->name . '<br />';
  echo 'User ID  : ' . $user->id . '<br />';

But is it possible to get the ID of any user based on username (or even better, the real name)? Note that I'm not looking for the current logged in user, but any given user.

Given only the users name, I want to echo something like this:

Jon Doe has user ID: 43
Jane Doe has user ID: 82

Can it be done?

1 Answer 1


Simple, you can use JUserHelper to achieve this:

$username = 'jophanpw';
$userId    = JUserHelper::getUserId($username);

echo $userId;

All the code above does is run the following database query:

public static function getUserId($username)
    $db = JFactory::getDbo();
    $query = $db->getQuery(true)
        ->where($db->quoteName('username') . ' = ' . $db->quote($username));
    $db->setQuery($query, 0, 1);

    return $db->loadResult();
  • Missed $ before userId at the second line of the first code
    – stckvrw
    Commented May 29, 2015 at 7:48
  • @stckvrw - Thanks for pointing that out ;) This is what happens when I answer questions too early in the morning
    – Lodder
    Commented May 29, 2015 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.