Prepared statement cannot be executed multiple times with integer values

这一生的挚爱 提交于 2019-11-29 10:59:38
seboettg

In case of prepared statements you have to use bindParam outside of loop, usually.

  1. bindParam is a single step
  2. setting bound variables is a repeatable step (loop)
  3. you have to run execute for each repetition

I guess, something like that would work:

$stmt = $pdoDB->prepare("INSERT INTO mytab (col, key) VALUES (:col, :key)");

// bind params (by reference)
$stmt->bindParams(":col", $col, PDO::PARAM_STR); //bind variable $col
$stmt->bindParams(":key", $key, PDO::PARAM_INT); //bind variable $key

$values = ['here','are','some','values'];
foreach ($values as $i => $value) {
    $col = $value; //set col
    $key = $i; //set key
    $stmt->execute();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!