有没有办法从 Zend 框架的查询中获取记录数? [英] Is there a way to get the number of records from a query with Zend-framework?

查看:27
本文介绍了有没有办法从 Zend 框架的查询中获取记录数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我在下面的通用选择,有没有办法使用 Zend Framework 获取从查询返回的记录数?循环中的 $row++ 对于我的解决方案是不可接受的,因为我使用的是分页(尽管它不在我的示例中).

Given my generic select below, is there a way to get the number of records returned from a query with Zend Framework? $row++ in a loop is not acceptable for my solution as I am using paging (though its not in my sample).

我也不想添加另一个带有Count(*)"的查询.

I also DO NOT want to add another query with "Count(*)".

$query = "Select * from Users where active = 1";
$stmt = $db->query($query);

$noOfRows = ???;

while ($row = $stmt->fetch())
{

  // processing
}

推荐答案

使用 fetchAll()

Use fetchAll()

fetchAll 返回一个数组,因此您可以执行以下操作:

fetchAll returns an array, so you can do something like this:

$rows = $db->fetchAll("select ...");
$numRows = sizeof($rows);
foreach ($rows as $row)
{
  // process each row
}

这篇关于有没有办法从 Zend 框架的查询中获取记录数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆