Jordyn Jordyn - 1 month ago 6
MySQL Question

PDO rowCount only returning one

I'm new to PDO. i have a SELECT qury and it is always returning one

$stmnt = $conn->prepare("SELECT post_title, posts_cat FROM posts WHERE posts_cat=:posts_cat");
$stmnt->execute(array(':posts_cat' => $cat_id));
$post_info = $stmnt->fetch();
$count = $stmnt->rowCount();


if there is no posts it is showing zero but if 1 or more then one it is displaying only 1

Can someone tell me the correct way to do this?

Appropriate your help.

Answer

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

rowCount() is not for a SELECT query, use a separate COUNT query for that or fetch all rows in an array and count its size

Comments