0

I am ver new to PDO, I am trying below code to get same value we used to get in mysql using "mysql_num_rows", but that is returning "1" where it should return "0":

Below function returns "1", it should return "0"

public function countdata($field, $table, $where = "1") {
    $ar = $this->pdo->prepare("SELECT COUNT(`" . $field . "`) FROM `" . $table . "` WHERE " . $where . "");
    $ar->execute();
    $cnt=$ar->fetchColumn();
    return $cnt;
}
Haren Sarma
  • 2,267
  • 6
  • 43
  • 72

2 Answers2

1

This question, as well as a superstition behind it, is a duplicate

You don't actually need neither mysql_num_rows, nor rowCount, nor this insecure function of yours. It is well-written, I have to admit - much better than usual stuff that posted here. But it bears an essential flaw - it doesn't support prepared statements - the only reason for using PDO, after all.

And, as it mentioned above - it is just useless. If you think it over, you will see that every time you want to have something from such a function, you already have it.

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-1

this souhld work:

public function countdata($field, $table, $where = "1") {
$ar = $this->pdo->prepare("SELECT COUNT(`" . $field . "`) FROM `" . $table . "` WHERE " . $where . "");
$ar->execute();
$cnt=$ar->rowCount();
return $cnt;

}

Mazeltov
  • 541
  • 5
  • 18