Connor Peet
Connor Peet

Reputation: 6265

Custom PDO Class Errors

EDIT: It turns out that I've been barking up the wrong tree so to speak. I've edited the question to make it at least useful for others who get the error.

For all those who get the following error when trying to execute a prepared PDO query:

Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined on line 26

See my answer below.

Upvotes: 3

Views: 139

Answers (2)

Connor Peet
Connor Peet

Reputation: 6265

That means you are passing more array values in $query->execture($array) than you have :values in the query. If you are getting a similar but differently worded error, it mean you have more :values in the query than you have in the array. For example:

$array = array(
  'username' => 'Bob',
  'id' => 42,
);

//...

$query = "SELECT * FROM `users` WHERE `id`=:id";

//...

$data->execute($array);

This would give an error because, although you are passing "username", it is never used in the query. Hope this helps someone!

Upvotes: 1

jeffjenx
jeffjenx

Reputation: 17457

Correct me if I'm wrong, but aren't you supposed to be using the following syntax for your parameter array:

array(':id' => 1)

Note the colon that is omitted in your original post.

See the PDO docs for examples.

Upvotes: 1

Related Questions