如何通过静态查询获取用户数?

时间:2018-02-11 06:50:18

标签: drupal-8

我想在Drupal 8中获得许多用户。

$connection = \Drupal::database();
$query = $connection->query('SELECT count(*)' FROM {users});
$results = $query->fetchAll();
$results = json_decode(json_encode($results),true);
print_r($results);

显示输出:

Array([0]=>Array([count(*)]=>10));

但我的网站目前只有2位用户。

这有什么不对?

1 个答案:

答案 0 :(得分:0)

此解决方案对我有用。

$sql = "SELECT name, quantity FROM {goods} WHERE vid = :vid";
$result = db_query($sql, array(':vid' => $vid));
if ($result) {
  while ($row = $result->fetchAssoc()) {
    // Do something with:
    // $row['name']
    // $row['quantity']
  }
}

$query = db_query("SELECT nid, title FROM {node}");
$records = $query->fetchAll();
foreach ($records as $record) {
  // Do something.
}