从 SQL 查询加载用户 - 正确的方法

发布于 2024-08-23 07:57:02 字数 1103 浏览 11 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云之铃。 2024-08-30 07:57:02

你的基本方法对我来说看起来很好,除了对 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文