使用 PHP 获取 Drupal 7 中的用户列表
如何使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
更好的方法
这将返回所有活动用户并使用函数中的代码将其用作帮助程序
A Better way
This will return all active users and use the code in a function to use it as a helper
Drupal 7 引入了实体的新概念。
用户现在也包含在实体中。
您可以使用以下函数entity_load()来获取所有实体的详细信息。
要获取数组中的用户名:
谢谢
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 :
Thanks
我的示例代码从 drupal 站点获取用户列表,
现在您可以从数组变量 $users 中获取答案。
例如,如果您想列出除管理员(默认用户)之外的所有用户,请使用以下示例:
在上面的两个示例中,每个用户的 id 都作为数组索引放置,并且保存每个用户的名称作为数组值。
My example code to get the list of users from drupal site,
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:
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.
试试这个:
Try this:
你也可以试试这个
You can try this as well,
使用 fetchAll()< 的简化示例/a>:
Simplified example with fetchAll():
这里是 Drupal 7 数据库 api 的完整文档
Here's the whole documentation for Drupal 7 Database api
让我们尝试一下
Lets give it a try
user_load_multiple
来自 module\profile\profile.pages.inc 的示例:
请注意,在 drupal 7 中,所有事情都通过实体发生。它不是那么快,但很灵活......
user_load_multiple
Example from modules\profile\profile.pages.inc :
Note that in drupal 7 all things happen via entity. It's not so fast, but flexible...
在Drupal 7中,您可以使用entity_load获取所有用户,
要单独获取用户名,请使用以下命令,
In Drupal 7 you can fetch all the users using entity_load,
To get the user names alone use the following,
如果您将 while 语句更改为读取
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
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.