Saturday 6 August 2011

Battling with PDO

I’ve been having a lot of problems with error HY093, and having the wrong number of parameters.

There are some blogs around that imply that you can pass an object converted to an array to the execute function documented below.

 http://php.net/manual/en/pdostatement.execute.php

$query = $dbh->prepare(
"INSERT INTO `myTable` (ID, Val1, Val2) "
."VALUES(:ID, :Val1, :Val2)");
$query->execute((array) $crud);


This does appear to work, and seems to occasionally work when there are more fields in the object than are in the query. 

 

Generally though it does not work.  Instead bind explicitly like this.


$query->bindParam(":ID", $crud->ID);
$query->bindParam(":Val1", $crud->Val1);
$query->bindParam(":Val2", $crud->Val2);
However, the documentation above states an FALSE will be returned if you bind more parameters than specified.

No comments:

Post a Comment