Jordyn Jordyn - 1 year ago 100
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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download