Drupal - 无法从db_query获取结果

时间:2015-03-04 11:51:20

标签: php mysql drupal drupal-7

我在drupal模块中编写了以下代码。

echo '<pre>';
$data = db_query("SELECT uid, name, status, created, access FROM {users} u WHERE uid <> 0 LIMIT 50 OFFSET 0");
print_r($data);
die;

但它并没有向我提供用户数据,而是给出了低于输出的数据:

DatabaseStatementBase Object
(
[dbh] => DatabaseConnection_mysql Object
    (
        [needsCleanup:protected] => 
        [target:protected] => default
        [key:protected] => default
        [logger:protected] => 
        [transactionLayers:protected] => Array
            (
            )

        [driverClasses:protected] => Array
            (
                [SelectQuery] => SelectQuery
            )

        [statementClass:protected] => DatabaseStatementBase
        [transactionSupport:protected] => 1
        [transactionalDDLSupport:protected] => 
        [temporaryNameIndex:protected] => 0
        [connectionOptions:protected] => Array
            (
                [database] => aaldev
                [username] => root
                [password] => 
                [host] => localhost
                [port] => 
                [driver] => mysql
                [prefix] => Array
                    (
                        [default] => 
                    )
            )

        [schema:protected] => 
        [prefixes:protected] => Array
            (
                [default] => 
            )

        [prefixSearch:protected] => Array
            (
                [0] => {
                [1] => }
            )

        [prefixReplace:protected] => Array
            (
                [0] => 
                [1] => 
            )
    )

[queryString] => SELECT uid, name, status, created, access FROM users u WHERE uid <> 0 LIMIT 50 OFFSET 0
)

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

假设您的查询正确且Drupal documentation,您有fetchAll()方法:

   $data = db_query("SELECT uid, name, status, created, access FROM {users} u WHERE uid <> 0 LIMIT 50 OFFSET 0");
   $data->fetchAll();

将所有记录检索到stdClass对象的索引数组中。