Sometimes, we want to get row count with PDO and PHP.
In this article, we’ll look at how to get row count with PDO and PHP.
How to get row count with PDO and PHP?
To get row count with PDO and PHP, we run a select count statement and then use the fetchColumn
method.
For instance, we write
$sql = "SELECT count(*) FROM `table` WHERE foo = ?";
$result = $con->prepare($sql);
$result->execute([$bar]);
$number_of_rows = $result->fetchColumn();
to call prepare
with the $sql
string.
Then we call execute
with the parameters for the prepared statement in an array.
And then we call fetchCoulmn
to get the value of count
returned by the SQL statement.
Conclusion
To get row count with PDO and PHP, we run a select count statement and then use the fetchColumn
method.