Get rows and count using PDO
UPDATE 1:
I've just noticed PDO::MYSQL_ATTR_FOUND_ROWS
in the manual, but I'm not sure how 开发者_高级运维to integrate it with the code below in the original question and/or if this will answer my question?
ORIGINAL QUESTION:
I currently have the following which works, it returns rows of data:
function my_function()
{
$DBH = new PDO( "mysql:host=server;dbname=database", "user", "pass" );
$DBH -> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$STH = $DBH -> prepare( "select * from table 1" );
$STH -> execute();
$ROWS = $STH -> fetchAll();
foreach($ROWS as $ROW) {
$output .= $ROW["col1"] . " - " . $ROW["col2"] . "<br />";
}
$DBH = null;
return $output;
}
How do I get a count of the number of rows returned? I've looked into PDO's rowCount() function, but that returns rows effected on update
, insert
, or delete
. I would like to get a count of the number of rows return from a select
.
I understand I can increment an integer i.e. i++ in the loop, but that seems a little primitive. I'm guessing there is a nicer way?
rowCount
returns the number of rows matched when used with a SELECT statement.
count($ROWS);
?
精彩评论