PHP PDO bindParam was falling in a foreach

前端 未结 3 1198
旧时难觅i
旧时难觅i 2020-12-05 03:15

I had a loop like that :

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

Nothing c

3条回答
  •  无人及你
    2020-12-05 03:42

    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):

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

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

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

提交回复
热议问题