更新时间:2023-02-26 11:03:11
此处的问题是您使用bindParam
绑定了参数,而bindParam
则通过引用进行绑定.在您的情况下,应改用bindValue
The problem here is that you binding parameters with bindParam
, which uses binding by reference. In your case you should use bindValue
instead:
foreach( $binders as $key => $value ) {
$sql->bindValue( $key, $value );
}
或者您可以将数组直接传递给execute()
方法:
Or you can pass your array directly to execute()
method:
$sql->execute( $binders );
如手册所述:
the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called.
因此,当您的foreach循环结束时,$value
具有最后一个数组项Apple
的值.因此,当execute
运行时,:ctid
和:p1
的值都将等于Apple
.当然,这不是您想要的东西
So when your foreach loop ends $value
has value of last array item Apple
. So when execute
runs, both :ctid
and :p1
values are becoming equal to Apple
. Surely, this is not what you want)