Just want to run this idea past any interested parties for thoughts on validity and correctness. Basic issue is I'm looking at some code that essentially runs the same query -- one that's pretty ugly and not terribly quick -- once to get the data to be displayed on the page (based on pagination offset/limit), then again to get the total count (so pagination knows how many pages there are. Here's the basic process I'm thinking of using to do it in one query:
<?php
// sample pagination values from some input or other
$offset = 20;
$numPerPage = 10;
$sql = "some complex query that takes more time than I'd like";
$stmt = $pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();
// to be used by pagination functions for total items:
$totalRows = $stmt->rowCount();
// populate array of data to be displayed on this page:
$data = array();
for($ix = 0; $ix++; $ix < $numPerPage) {
$row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_ABS, $offset + $ix);
if($row != false) {
$data[] = $row;
}
else {
break;
}
}
Thoughts? Suggestions?