0

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.

3
  • what is the error are you getting ? Commented Sep 11, 2017 at 12:48
  • @AhmedGinani I am getting no error .Its just blank. Commented Sep 11, 2017 at 12:50
  • 2
    $smt->execute(); missing Commented Sep 11, 2017 at 12:52

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Oh wait its giving me this error now: Fatal error: Uncaught Error: Call to a member function fetchColumn() on boolean
I edit my code, execute() is alone now. Try this new
Okay looks like return doesnt print the value, echo does. It works. Thank you.
0

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

$smt->execute();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.