Php 有没有办法告诉Drupal7中SelectQuery结果中有多少行?

Php 有没有办法告诉Drupal7中SelectQuery结果中有多少行?,php,mysql,drupal,drupal-7,Php,Mysql,Drupal,Drupal 7,我真正想知道的是我的查询是否返回了结果,但实际的行计数值会更好 例如: $query = db_select('node', 'n'); $query->fields('n', array('nid', 'title', 'status')); 您正在寻找: 在您的示例中,您可以使用: $query = db_select('node', 'n'); $query->fields('n', array('nid', 'title', 'status')); $num_rows=$

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

例如:

$query = db_select('node', 'n');
$query->fields('n', array('nid', 'title', 'status'));
您正在寻找:

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

$query = db_select('node', 'n');
$query->fields('n', array('nid', 'title', 'status'));
$num_rows=$query->countQuery()->execute()->fetchField();
要计算结果并随后获取结果,请执行以下操作:

$result=$query->execute();
您正在寻找:

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

$query = db_select('node', 'n');
$query->fields('n', array('nid', 'title', 'status'));
$num_rows=$query->countQuery()->execute()->fetchField();
要计算结果并随后获取结果,请执行以下操作:

$result=$query->execute();

您可以使用查询的方便的
fetchAll()
方法将所有结果加载到一个数组中,然后按正常方式计算数组。这将导致只执行一个查询:

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

$count = count($results);

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

您可以使用查询的方便的
fetchAll()
方法将所有结果加载到一个数组中,然后按正常方式计算数组。这将导致只执行一个查询:

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

$count = count($results);

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

我认为用这个来代替更有效

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

我认为用这个来代替更有效

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

所以没有简单的方法可以直接从SelectQuery结果中执行?真的不想查询数据库两次不构建另一个查询对象:(我认为您不需要构建另一个查询对象-您可以在countQuery之后使用现有的查询对象(请参见编辑的答案),但当然,在这个解决方案中,sql会执行两次…因此没有简单的方法直接从SelectQuery结果执行?真的不想查询数据库两次不构建另一个查询对象:(我认为您不需要构建另一个查询对象-您可以在countQuery之后使用现有的查询对象(请参见编辑的答案),但当然,在这个解决方案中,sql会执行两次…谢谢!假设有某种方法可以将内容加载到数组:DThanks!假设有某种方法可以将内容加载到数组:D