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); } ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With