Herry Potei
Herry Potei

Reputation: 179

PHP : not able to return sql count value

I am a newbie in PHP programming. Here is my code :

class AdminActions extends DBManager{
    public function loginUser($username, $password){
        $dbh = $this->getConnection();
        $smt = $dbh->prepare("SELECT count(1) FROM admin WHERE admin_name=:username AND admin_password=:password"); 
        $smt->bindValue(':username', $username);
        $smt->bindValue(':password', $password);
        $count = $smt->fetchColumn();
        return $count;
    }
}

I am not able to return the value of $count. There is no error to be displayed (no output). I would like to have answers containing the function fetchColumn() . Thank you in advance.

Upvotes: 0

Views: 39

Answers (2)

Ahmed Ginani
Ahmed Ginani

Reputation: 6650

As per document you have to execute pdo statement. you have to execute your query.

$smt->execute();

Upvotes: 0

David Courtey
David Courtey

Reputation: 139

You forget to exec the request and to select what column you wanna count. Try :

class AdminActions extends DBManager{
public function loginUser($username, $password){
    $dbh = $this->getConnection();
    $smt = $dbh->prepare("SELECT count(id) FROM admin WHERE admin_name=:username AND admin_password=:password"); 
    $smt->bindValue(':username', $username);
    $smt->bindValue(':password', $password);
    $smt->execute();
    return $smt->fetchColumn();
}

From : Row count with PDO

Upvotes: 2

Related Questions