note 117873 added to pdostatement.rowcount
| From: | Anonymous | Date: | Mon, 24 Aug 2015 20:51:32 +0000 |
| Subject: | note 117873 added to pdostatement.rowcount | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203450@lists.php.net to get a copy of this message | ||
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);
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 84.219.143.167
----
Manual Page -- http://php.net/manual/en/pdostatement.rowcount.php
Edit -- https://master.php.net/note/edit/117873
Del: integrated -- https://master.php.net/note/delete/117873/integrated
Del: useless -- https://master.php.net/note/delete/117873/useless
Del: bad code -- https://master.php.net/note/delete/117873/bad+code
Del: spam -- https://master.php.net/note/delete/117873/spam
Del: non-english -- https://master.php.net/note/delete/117873/non-english
Del: in docs -- https://master.php.net/note/delete/117873/in+docs
Del: other reasons-- https://master.php.net/note/delete/117873
Reject -- https://master.php.net/note/reject/117873
Search -- https://master.php.net/manage/user-notes.php