使用 PHP 获取 Drupal 7 中的用户列表

发布于 2024-12-20 16:02:26 字数 482 浏览 3 评论 0原文

如何使用 PHP 从 Drupal 7 数据库中获取用户名?

看看这个: 是否可以使用 Drupal api 获取用户列表?

这是针对 Drupal 6 的,我已经尝试过了,但它说

Call to undefined function db_fetch_object()

这是我的代码:

$result = db_query("SELECT name AS users_name FROM {users}");
while($row = db_fetch_object($result)) {    
        print_r($row);      
}

How can I fetch user names from a Drupal 7 database using PHP?

See this:
Is it possible to use the Drupal api to get a list of users?

This is for Drupal 6 and I have tried this but it says

Call to undefined function db_fetch_object()

Here is my code:

$result = db_query("SELECT name AS users_name FROM {users}");
while($row = db_fetch_object($result)) {    
        print_r($row);      
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(11

电影里的梦 2024-12-27 16:02:27

更好的方法

function GetAllActiveUsers() {
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'user')
        ->propertyCondition('status', 1);
  $results = $query->execute();
  $uids = array_keys($results['user']);
  return $uids;
}

这将返回所有活动用户并使用函数中的代码将其用作帮助程序

A Better way

function GetAllActiveUsers() {
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'user')
        ->propertyCondition('status', 1);
  $results = $query->execute();
  $uids = array_keys($results['user']);
  return $uids;
}

This will return all active users and use the code in a function to use it as a helper

那一片橙海, 2024-12-27 16:02:27

Drupal 7 引入了实体的新概念。
用户现在也包含在实体中。
您可以使用以下函数entity_load()来获取所有实体的详细信息。

要获取数组中的用户名:

$users = entity_load('user');
$usernames = array();
foreach($users as $id => $user){
    $usernames[$user->uid] = $user->name;
}
print_r($usernames);

谢谢

Drupal 7 introduces a new concept of Entity.
User also included in Entity now.
You can use following function entity_load() to get the details of all entities.

To fetch the usernames in array :

$users = entity_load('user');
$usernames = array();
foreach($users as $id => $user){
    $usernames[$user->uid] = $user->name;
}
print_r($usernames);

Thanks

撩起发的微风 2024-12-27 16:02:27

我的示例代码从 drupal 站点获取用户列表,

$all_users = entity_load('user');
foreach($all_users as $value) {
  $user_list = (array)$value;
  $users[$user_list['uid']] = $user_list['name'];
}

现在您可以从数组变量 $users 中获取答案。

例如,如果您想列出除管理员(默认用户)之外的所有用户,请使用以下示例:

$all_users = entity_load('user');
foreach($all_users as $value) {
  $user_list = (array)$value;
  if($user_list['uid'] > 1) {
    $users[$user_list['uid']] = $user_list['name'];
  }
}

在上面的两个示例中,每个用户的 id 都作为数组索引放置,并且保存每个用户的名称作为数组值。

My example code to get the list of users from drupal site,

$all_users = entity_load('user');
foreach($all_users as $value) {
  $user_list = (array)$value;
  $users[$user_list['uid']] = $user_list['name'];
}

Now you can get the answer from the array variable $users.

For example if you want to list all the user other than administrator (default user) means use the below example:

$all_users = entity_load('user');
foreach($all_users as $value) {
  $user_list = (array)$value;
  if($user_list['uid'] > 1) {
    $users[$user_list['uid']] = $user_list['name'];
  }
}

In the above both example, the id of each user is placed as array index and the name of each usder is saved as array value.

私野 2024-12-27 16:02:27

试试这个:

$query = db_select('users', 'u')
->fields('u')
->execute();
while($result = $query->fetchAssoc()) {
     print_r($record['name']);
}

Try this:

$query = db_select('users', 'u')
->fields('u')
->execute();
while($result = $query->fetchAssoc()) {
     print_r($record['name']);
}
半岛未凉 2024-12-27 16:02:27

你也可以试试这个

$query = db_select('users', 'u');
    $query->fields('u', array('uid, name'));
    $result = $query->execute();
    while($record = $result->fetchAssoc()) {
         $account = user_load($record['uid']);
         if($account->uid){
             print $account->name;
         }
    }

You can try this as well,

$query = db_select('users', 'u');
    $query->fields('u', array('uid, name'));
    $result = $query->execute();
    while($record = $result->fetchAssoc()) {
         $account = user_load($record['uid']);
         if($account->uid){
             print $account->name;
         }
    }
骄傲 2024-12-27 16:02:27

使用 fetchAll()< 的简化示例/a>:

 $users = db_select('users', 'u')
    ->fields('u', array('uid'))
    ->execute()
    ->fetchAll();

 foreach ($users as $user) {
    $full_user = user_load($user->uid);
    //do your stuff here 
 }

Simplified example with fetchAll():

 $users = db_select('users', 'u')
    ->fields('u', array('uid'))
    ->execute()
    ->fetchAll();

 foreach ($users as $user) {
    $full_user = user_load($user->uid);
    //do your stuff here 
 }
淑女气质 2024-12-27 16:02:26
$query = db_select('users', 'u');

$query
  ->condition('u.uid', 0, '<>')
  ->fields('u', array('name'));

$result = $query->execute();

这里是 Drupal 7 数据库 api 的完整文档

$query = db_select('users', 'u');

$query
  ->condition('u.uid', 0, '<>')
  ->fields('u', array('name'));

$result = $query->execute();

Here's the whole documentation for Drupal 7 Database api

年华零落成诗 2024-12-27 16:02:26

让我们尝试一下

$query = db_select('users', 'u');
    $query->fields('u', array('name'));
    $result = $query->execute();
    while($record = $result->fetchAssoc()) {
         print_r($record['name']);
    }

Lets give it a try

$query = db_select('users', 'u');
    $query->fields('u', array('name'));
    $result = $query->execute();
    while($record = $result->fetchAssoc()) {
         print_r($record['name']);
    }
唱一曲作罢 2024-12-27 16:02:26

user_load_multiple
来自 module\profile\profile.pages.inc 的示例:

$users = user_load_multiple($uids);

$content = '';
foreach ($users as $account) {
  $profile = _profile_update_user_fields($fields, $account);
  $content .= theme('profile_listing', array('account' => $account, 'fields' => $profile));
}

请注意,在 drupal 7 中,所有事情都通过实体发生。它不是那么快,但很灵活......

user_load_multiple
Example from modules\profile\profile.pages.inc :

$users = user_load_multiple($uids);

$content = '';
foreach ($users as $account) {
  $profile = _profile_update_user_fields($fields, $account);
  $content .= theme('profile_listing', array('account' => $account, 'fields' => $profile));
}

Note that in drupal 7 all things happen via entity. It's not so fast, but flexible...

吃不饱 2024-12-27 16:02:26

在Drupal 7中,您可以使用entity_load获取所有用户,

要单独获取用户名,请使用以下命令,

$users = entity_load('user');
$user_names = array();
foreach ($users as $user_id => $user) {
  $user_names[] = $user->name;
}

In Drupal 7 you can fetch all the users using entity_load,

To get the user names alone use the following,

$users = entity_load('user');
$user_names = array();
foreach ($users as $user_id => $user) {
  $user_names[] = $user->name;
}
另类 2024-12-27 16:02:26

如果您将 while 语句更改为读取

while ($result as $row) 

d7 中不再需要 db_fetch_object ,

那么您的代码几乎是正确的,它会起作用。尽管本文中指定的 db_select 调用可以工作,但它们需要更多开销,除非您尝试生成动态查询,否则应避免使用。另请参阅:http://drupal.org/node/224333 了解有关 api 如何在d6 和 d7。在此页面上搜索 db_fetch_object 将给出此信息。

Your code was almost correct if you had changed your while statement to read

while ($result as $row) 

db_fetch_object is no longer needed in d7

it would have worked. Although db_select calls specified in this post will work, they require more overhead and should be avoided unless you are trying to generate dynamic queries. Also see: http://drupal.org/node/224333 for info on how the apis have chaned between d6 and d7. A search for db_fetch_object on this page would've given this info.

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