Loading users from an SQL query - the correct way
I have an SQL query that lists the uid
of all users who have a certain role:
SELECT u.uid
FROM {users} as u, {users_roles} as ur
WHERE u.uid = ur.uid AND ur.rid = 10 ORDER BY u.uid DESC
I need to load them all in a开发者_StackOverflown array as objects for listing.
I asked a question previously that left me only with the answer that what I wanted to do would be easier done without Views - so I'm going use a template file instead.
Therefore this question.I know how to do this, but apparently my method is worth -2
This is how I want to do it:
$research['sql'] = "SELECT u.uid FROM {users} as u, {users_roles} as ur WHERE u.uid = ur.uid AND ur.rid = 10 ORDER BY u.uid DESC";
$research['sql_result'] = db_query($alumni['sql']);
// Load user objects and store in array
while($user_array = db_fetch_array($research['sql_result'])) {
// Create user objets based on uid
$user_obj = user_load($user_array['uid']);
// Load profile
profile_load_profile($user_obj);
$research['users'][$user_obj->uid] = $user_obj;
}
Please help me with how I should do it.
Your basic approach looks fine by me, except that the call to profile_load_profile()
is redundant:
The user_load()
function will invoke hook_user
with operation 'load', and the profile module implements hook_user
and calls profile_load_profile()
for the load operation itself, so by the time you call it explicitly, it has already been called implicitly and you can just drop it.
精彩评论