且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PDO-查询未给出结果

更新时间: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)