Sometimes, we want to get row count with PDO with PHP.
In this article, we’ll look at how to get row count with PDO with PHP.
How to get row count with PDO with PHP?
To get row count with PDO with PHP, we can run a select count query.
For instance, we write
$sql = "SELECT count(*) FROM `table` WHERE foo = ?";
$result = $con->prepare($sql);
$result->execute([$bar]);
$number_of_rows = $result->fetchColumn();
to run the $sql
query that selects the count from the table table.
The $sql
string is converted to a prepared statement with prepare
.
We run the query with execute
.
We then get the value from $result->fetchColumn()
.
Conclusion
To get row count with PDO with PHP, we can run a select count query.