user1469270
user1469270

Reputation:

PDOStatement object with bindparam [PHP]

I have a method that I'm getting to see if a user is exists:

public function login_user($user_name, $user_password){
    $this->statement = $this->conn->prepare('SELECT * FROM users WHERE user_name=:user_name AND user_password=:user_password');
    $this->statement = $this->conn->bindParam(':user_name', $user_name);
    $this->statement = $this->conn->bindParam(':user_password', $user_password);

    $this->statement->execute();
    return $this->statement->fetch(PDO::FETCH_ASSOC);
}

I have never used PDO before and I'm slightly confused. I am getting the error:

Call to undefined method PDO::bindParam().

I have seen an answer saying it's because it is part of the PDOStatement class.

By changing my code to this (removing $this->conn) fixes it:

$this->statement->bindParam(':user_name', $user_name);
$this->statement->bindParam(':user_password', $user_password);

However, I have no idea why? $this->conn is by PDO object. What have I just done to make this work?

Upvotes: 1

Views: 209

Answers (2)

meda
meda

Reputation: 45490

make the following changes to your function:

public function login_user($user_name, $user_password){

 //prepare the query
 $query='SELECT * FROM users WHERE user_name=:user_name AND user_password=:user_password';
 $statement = $this->conn->prepare($query);

 //bind the parameters
 $statement->bindParam(':user_name', $user_name);
 $statement->bindParam(':user_password', $user_password);

 //excute & fetch the data
 $statement->execute();
 $result = $statement->fetch(PDO::FETCH_ASSOC);

 return $result;
}

Upvotes: 0

Jacob
Jacob

Reputation: 920

just do

$this->statement->bindParam()

to bind your parameters, then you can call your execute statement

Upvotes: 2

Related Questions