Alexis Tyler
Alexis Tyler

Reputation: 949

PDO fetchColumn not returning row count

I have a table in my database called posts it contains a row and yet when I do a count on it I'm not getting anything back, not 0 just empty. $numRows contains nothing at all when I echo it out. If you need more info can you please comment and don't just down vote this question.

My question: Is there any reason why this wouldn't work? I'm new to PDO and I'm not sure if this is the way to correctly count the rows.

$id = 1;
$STH_ = $DBH->prepare("SELECT COUNT(*) FROM posts WHERE owner=:owner");
$STH_->bindParam(':owner', $id);
$numRows = $STH_->fetchColumn();

Upvotes: 0

Views: 444

Answers (2)

Cfreak
Cfreak

Reputation: 19309

You need to call $STH_->execute(); to actually run the query. Call it after your call to bindParam()

You should also check for errors.

Upvotes: 2

Haim Evgi
Haim Evgi

Reputation: 125496

you need to execute the query first like

$STH_->execute();

Upvotes: 1

Related Questions