douyue5856 2010-08-25 20:25
浏览 28
已采纳

数组具有比PDO准备语句更多的参数错误

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 values 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?

  • 写回答

1条回答 默认 最新

  • dougaopu7938 2010-08-25 21:07
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?