note 117873 deleted from pdostatement.rowcount by cmb
| From: | cmb@php.net | Date: | Mon, 24 Aug 2015 23:00:59 +0000 |
| Subject: | note 117873 deleted from pdostatement.rowcount by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-203455@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
To get the count of your SELECT query with just one query (not having to do one query to fetch and
one query to count) you can iterate over the PDOStatement object with an foreach loop and increment
a counter.
<?php
foreach ($stmt as $row) {
$count++;
print_r($row);
}
echo "Rows: ", $count;
?>
Note however that once you iterated over the object once, you cannot iterate over it again.
So if you need the row count before the rows you need to add each row to an array.
<?php
$rows = [];
foreach ($stmt as $row) {
$count++;
array_push($rows, $row);
}
echo "Rows: ", $count;
foreach ($rows as $row) {
print_r($row);
}
?>