A couple of ways. You can try the PDOStatement::rowCount property, but note the caveat described on that page which is that not all database systems will return the number of rows after a SELECT statement. Instead, they suggest executing a SELECT COUNT(*) first, followed by the SELECT.
Personally, if you need to count the rows before parsing each one, I would do something like this:
If you don't need the count upfront, then:PHP Code:if ($st->execute() === true)
{
$rows = $st->fetchAll(PDO::FETCH_ASSOC);
echo count($rows);
}
PHP Code:$count = 0;
if ($st->execute() === true)
{
while ($row = $st->fetch(PDO::FETCH_ASSOC))
{
++$count;
// ...
}
}




Reply With Quote