数组的参数多于 PDO 准备好的语句会导致错误
如果必须执行多个查询。有些参数重叠,有些则不重叠。
我想创建一个数组,其中包含所有查询的所有参数的数据。
我想如果数组包含准备好的语句不包含的值,它会忽略它们,但它给了我这个错误:
无效参数数量:数量 绑定变量与数字不匹配 代币数量
就是我的意思:
$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);
有没有办法允许这样做?或者每次都必须创建不同的数组?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这不受支持。
顺便说一句,在维护方面,我总是觉得使用 bindParam 方法会产生更具可读性的代码。像这样...
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...