0

I am using PDO with OOP. I have a code like this:

public function selectOne($id) {
    $sql = "SELECT * FROM emps WHERE id = ':id'";
    $stmt = $this->connect()->prepare($sql);
    $stmt->bindValue(":id", $id);
    $stmtExec = $stmt->execute();
    if ($stmtExec->rowCount() > 0) {
        echo "got results";
    } else {
        echo 'nothing';
    }
}

When I try to run this it gives me an error: Fatal error: Uncaught Error: Call to a member function rowCount() on boolean

Where am I doing wrong. I am new to Php. Thanks!

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Rakesh Kohali
  • 275
  • 2
  • 5
  • 17

1 Answers1

1

There is no need for the single quotes around the named placeholder :id.

$sql = "SELECT * FROM emps WHERE id = :id";

EDIT: Also, like correctly stated by @Jon Stirling, $stmt->execute() returns a boolean. Method rowCount() should be executed on the PDOStatement object instead (your $stmt variable).

http://php.net/manual/en/pdostatement.rowcount.php

Zoli Szabó
  • 4,366
  • 1
  • 13
  • 19