有没有办法告诉 Drupal 7 中 SelectQuery 结果中有多少行?

问题描述 投票:0回答:4

我真正想知道的是我的查询是否返回结果,但实际的行计数值会很好。

例子:

$query = db_select('node', 'n');
$query->fields('n', array('nid', 'title', 'status'));
php mysql drupal drupal-7
4个回答
2
投票

您正在寻找:

count查询()

在您的示例中,您可以使用:

$query = db_select('node', 'n');
$query->fields('n', array('nid', 'title', 'status'));
$num_rows=$query->countQuery()->execute()->fetchField();

计算结果,然后获取它们:

$result=$query->execute();

1
投票

您可以使用方便的查询

fetchAll()
方法将所有结果加载到数组中,然后正常计算数组。这将导致只执行一个查询:

$query = db_select('node', 'n');
$query->fields('n', array('nid', 'title', 'status'));
$results = $query->execute()->fetchAll();

$count = count($results);

foreach ($results as $result) {
  // ...
}

0
投票

我认为尝试这个更有效。

$result = db_select('table_name' ,'t');
$result->fields('t')->execute();
$num_of_results = $result->rowCount();

0
投票
db_select('table', 'alias')->fields(NULL, array('a_field'))->countQuery()->execute()->fetchField();
© www.soinside.com 2019 - 2024. All rights reserved.