Search code examples
phpooppdo

Error in my PDO fetch column function: "Call to a member function fetchColumn() on a non-object"


I am using PDO object for executing query in PHP:

$stmt = $this->db->prepare('select email from users where email = :email');
return (bool) $stmt->execute(array(':email' => $email))->fetchColumn();

I am getting error here:

Fatal error: Call to a member function fetchColumn() on a non-object

What is the problem? The $this->db->query() command is working.


Solution

  • PDOStatement::execute returns a boolean indicating whether or not the query succeeded. To fetch results, you must call a fetch method on the original statement instance.