-3

I get error on this code.. I set UserName and Password but i get error. What is the my fail ?

$Id=$db->prepare("select UserID from Users where 1=1 and IsActive=1 and Name=:UserName and Password=:Password");
$Id->bindParam(":UserName",$UserName,PDO::PARAM_STR);
$Id->bindParam(":Password",$Password,PDO::PARAM_STR);
$Id->execute();

This is error:

PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • Are you sure `$UserName` and `$Password` are both set? If they are, I do not see any reason why it would cause that error. The [first example](http://php.net/manual/en/pdostatement.bindparam.php#refsect1-pdostatement.bindparam-examples) in the documentation should be the same thing you are doing. – Mikey Oct 16 '16 at 13:13

1 Answers1

0

Honestly, I've never had any reason to use bindParam or bindValue because you can simply pass an associative array into execute` function as shown below:

$Id = $db->prepare("select UserID from Users where 1=1 and IsActive=1 and Name=:UserName and Password=:Password");
$Id->execute(array(":UserName" => $UserName, ":Password" => $Password));
Mikey
  • 6,728
  • 4
  • 22
  • 45