沃梦达 / 编程问答 / php问题 / 正文

PHP PDO bindParam 陷入了 foreach

PHP PDO bindParam was falling in a foreach(PHP PDO bindParam 陷入了 foreach)

本文介绍了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):

 &$val){$sth->bindParam($key, $val);}?>

<块引用>

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

 $val) {$sth->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 :

Vili 28-May-2010 12:01

This works ($val by reference):

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

This will fail ($val by value, because bindParam needs &$variable):

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

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

本文标题为:PHP PDO bindParam 陷入了 foreach

基础教程推荐