How To Load Another Users Profile Data In Drupal

Drupals built in function for loading profile, profile_load_profile(), loads profile information but designed for the currently logged in user. If you want to grab the profile data for another user, well, if there's a Drupal function for it, I haven't found it yet.


So what to do in a case where you want to load another users profile data? Code it! Add the following to your theme's template.tpl.php file and replace theme with the name of your theme:

function theme_get_user_profile( $uid ){

    $result = db_query( "SELECT f.name, v.value
    FROM {profile_fields} f INNER JOIN {profile_values}
    v ON f.fid = v.fid WHERE uid = %d AND f.name = '%s'",
    $uid, "profile_bio" );
    $_bio = db_fetch_object( $result );

    $result = db_query( "SELECT f.name, v.value
    FROM {profile_fields} f INNER JOIN {profile_values}
    v ON f.fid = v.fid WHERE uid = %d AND f.name = '%s'",
    $uid, "profile_fullname" );
    $_name = db_fetch_object( $result );

    $result = db_query( "SELECT f.name, v.value
    FROM {profile_fields} f INNER JOIN {profile_values}
    v ON f.fid = v.fid WHERE uid = %d AND f.name = '%s'",
    $uid, "profile_city" );
    $_city = db_fetch_object( $result );

    $result = db_query( "SELECT f.name, v.value
    FROM {profile_fields} f INNER JOIN {profile_values}
    v ON f.fid = v.fid WHERE uid = %d AND f.name = '%s'",
    $uid, "profile_state" );
    $_state = db_fetch_object( $result );

    return array( 'name' => $_name->value,
    'bio' => $_bio->value, 'city' => $_city->value,
    'state' => $_state->value );
}


So, to use it pass the uid from the currently loaded node like $_user = theme_get_user_profile( $node->uid );
(should be the node's author uid) and you will get back an aray that contains the values you need, $_user['name'], $_user['bio'], $_user['city'] and $_user['state']