本文介绍了PHP PDO bindParam 陷入了 foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的循环:

foreach($Fields as $Name => $Value){$Query->bindParam(':'.$Name, $Value, PDO::PARAM_STR);}

没什么复杂的.但是,每个值都设置为数组中的最后一个 ($Fields).

我该如何解决?

解决方案

然而,感谢这个 伙计们.我发现您需要在之前使用 & 通过引用传递值:

foreach($Fields as $Name => &$Value){$Query->bindParam(':'.$Name, $Value, PDO::PARAM_STR);}

这让我发疯了.

来自 PHP.net 的实际报价:

Vili 2010 年 5 月 28 日 12:01

这有效(参考 $val):

bindParam($key, $val);}?>

这会失败($val by value,因为bindParam需要&$variable):

bindParam($key, $val);}?>

I had a loop like that :

foreach($Fields as $Name => $Value){
    $Query->bindParam(':'.$Name, $Value, PDO::PARAM_STR);
}

Nothing complicated. However, each value was set to the last one in the array ($Fields).

How can I fix that ?

解决方案

However, thanks to this guys. I found out that you need to pass the value by reference with a & before like that :

foreach($Fields as $Name => &$Value){
    $Query->bindParam(':'.$Name, $Value, PDO::PARAM_STR);
}

This was driving me nuts.

Actual quote from PHP.net :

<?php
foreach ($params as $key => &$val){
    $sth->bindParam($key, $val);
}
?>
<?php
foreach ($params as $key => $val) {
    $sth->bindParam($key, $val);
}
?>

这篇关于PHP PDO bindParam 陷入了 foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:15