从 SQL 查询加载用户 - 正确的方法
我有一个 SQL 查询,列出了具有特定角色的所有用户的 uid
:
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
我需要将它们全部加载到一个数组中作为列出的对象。
我之前问过一个问题,但我只得到了答案我想要做的事情在没有视图的情况下会更容易完成 - 所以我将使用模板文件。
所以这个问题。
我知道如何做到这一点,但显然我的 方法值得 -2
这我想要这样做:
$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;
}
请帮助我应该如何做。
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 an 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的基本方法对我来说看起来很好,除了对
profile_load_profile()
的调用是多余的:user_load()
函数将通过操作调用hook_user
'load',并且 profile 模块实现了hook_user
并为加载操作本身调用profile_load_profile()
,因此当您显式调用它时,它已经被隐式调用你可以把它扔掉。Your basic approach looks fine by me, except that the call to
profile_load_profile()
is redundant:The
user_load()
function will invokehook_user
with operation 'load', and the profile module implementshook_user
and callsprofile_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.