2
$statement = $connection->prepare("INSERT INTO test(emailAddress, password) VALUES(:emailAddress, :password)");
$data = array("emailAddress" => "test1", "password" => "test", "abc" => 123);

if($statement->execute($data)) {
    echo $connection->lastInsertId();
} else {
    echo "error";
}

I know the number of tokens are more than the required, but sometimes my array could have more and I need them.

Invalid parameter number: number of bound variables does not match number of tokens'

Can PDO ignore extra parameters?

user2727195
  • 7,122
  • 17
  • 70
  • 118

1 Answers1

2

No, PDO requires that you have the same number of values as the number of parameters in the prepared query.

Learn to use array_intersect_key():

$keys = array("emailAddress", "password");
$data = array("emailAddress" => "test1", "password" => "test", "abc" => 123);

if($statement->execute(array_intersect_key($data, array_flip($keys)))) {
    . . .
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828