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).
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
}
Kekoa is wrong.
If you put that SQL_CALC_FOUND_ROWS and then if you put a limit you will get the full number of rows (is no affected by the limit).
If you have a simple table then is recomended to use count(*) in the pagination since is much faster, but if you have complex queries and your data is the result of many joins and so on then my advice is to use SQL_CALC_FOUND_ROWS.
I hope this helps you.
BornForCode
Use fetchAll()
fetchAll returns an array, so you can do something like this:
It still requires another query, but with MySQL there is a SELECT parameter
SQL_CALC_FOUND_ROWS
- it stores a value in your connection that you can thenSELECT FOUND_ROWS()
Note - the performance of your initial query will go down, because the limit will be processed later, but it may be faster than a separate COUNT().
FOUND_ROWS()
If you are returning the entire result set in your SQL query, you can do a
fetchAll()
instead of afetch()
, and thencount()
the number of items in the array returned fromfetchAll()
.However, if you are doing paging with a MySQL
LIMIT
clause or equivalent, you will only get the count for number of items returned for that query(page). There's no way to get the entire count of results without doing aCOUNT()
in SQL in this case. You can only do calculations based on the resultset from the database.It's strange but this just works:
i should mention that i use this line in my config file:
here is some code for feather ideas; however it does not work:
also take a look at http://old.nabble.com/RE:-CALC_FOUND_ROWS-vs.-count%28*%29-p16761518.html