【问题标题】:getting list of users in Drupal 7 using PHP使用 PHP 在 Drupal 7 中获取用户列表
【发布时间】:2011-12-10 09:50:33
【问题描述】:

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

看到这个: Is it possible to use the Drupal api to get a list of users?

这是 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);      
}

【问题讨论】:

    标签: php drupal drupal-7 drupal-modules


    【解决方案1】:

    让我们试一试

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

    【讨论】:

    【解决方案2】:

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

    要单独获取用户名,请使用以下内容,

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

    【讨论】:

    • 实际上这个函数会产生一个问题,它会改变当前的用户会话。自动加载另一个用户会话。
    【解决方案3】:

    user_load_multiple
    来自 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));
    }
    

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

    【讨论】:

      【解决方案4】:
      $query = db_select('users', 'u');
      
      $query
        ->condition('u.uid', 0, '<>')
        ->fields('u', array('name'));
      
      $result = $query->execute();
      

      Here 是 Drupal 7 数据库 API 的完整文档

      【讨论】:

        【解决方案5】:

        如果您将 while 语句更改为 read,您的代码几乎是正确的

        while ($result as $row) 
        

        d7 中不再需要 db_fetch_object

        它会起作用的。尽管本文中指定的 db_select 调用会起作用,但它们需要更多开销并且应该避免,除非您尝试生成动态查询。另请参阅:http://drupal.org/node/224333 了解 api 如何在 d6 和 d7 之间转换的信息。在此页面上搜索 db_fetch_object 将提供此信息。

        【讨论】:

          【解决方案6】:

          我从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被放置为数组索引,每个用户的名字被保存为数组值。

          【讨论】:

            【解决方案7】:

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

            获取数组中的用户名:

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

            谢谢

            【讨论】:

              【解决方案8】:

              更好的方法

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

              这将返回所有活跃用户并使用函数中的代码将其用作助手

              【讨论】:

                【解决方案9】:

                试试这个:

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

                【讨论】:

                  【解决方案10】:

                  你也可以试试这个,

                  $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;
                           }
                      }
                  

                  【讨论】:

                    【解决方案11】:

                    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 
                     }
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2011-05-13
                      • 2015-01-16
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2013-05-28
                      • 1970-01-01
                      相关资源
                      最近更新 更多