开发者

Array has more params than PDO prepared statement causes error

If Have to execute several queries. Some of the parameters overlap, some do not.

I wanted to create one array containing the data for all the params for all the queries.

I figured if the array contains va开发者_运维技巧lues that the prepared statement does not, it would ignore them but its giving me this error:

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

here is what I mean:

$data = array( 'a' => $a, 'b' => $b, 'c' => $c, 'd' => $d);

$data['e'] = "e";
$STH = $this->PDO->prepare("INSERT INTO table1 ( fieldA, fieldB, fieldE ) VALUES (:a, :b, :e )");
$STH->execute($data);

$data['f'] = "f";
$STH = $this->PDO->prepare("INSERT INTO table2 ( fieldA, fieldD, fieldF ) VALUES (:a, :d, :f )");
$STH->execute($data);

Is there a way to allow this? or do have to create a different array each time?


No, that's not supported.

BTW, when it comes to maintaining I always felt using the bindParam method results in much more readable code. Like this...

$STH = $this->PDO->prepare("INSERT INTO table1 ( fieldA, fieldB, fieldE ) VALUES (:a, :b, :e )");
$STH->bindParam(':a', 'a');
$STH->bindParam(':b', 'b');
$STH->bindParam(':e', 'e');
$STH->execute();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜