在for / foreach循环中绑定pdo变量

时间:2018-04-08 17:31:22

标签: php pdo foreach

我已经看过其他一些Stack Overflow问题,但是我无法弄清楚如何循环遍历sql语句并将变量绑定到该语句。以下是我的查询现在的样子:

$db = dbCon();
$sql = "";
foreach($data as $d) {
    $sql .= "INSERT INTO required_fields (field_name) VALUES ($d)";
}
$stmt = $db->prepare($sql);
// Here is where I need to bind the variables
$stmt->execute();
$rowsChanged = $stmt->rowCount();
$stmt->closeCursor();
return $rowsChanged;

我无法弄清楚如何循环,因为这是我理解bindValue的工作方式:

$stmt->bindValue(':variableInQuery', $PHPVariable, PDO::PARAM_STR);

让我感到震惊的是变量前面的冒号,我无法弄清楚如何使php变量名称独一无二......我缺少什么?

1 个答案:

答案 0 :(得分:1)

您需要在循环中移动代码:

val dataMain = df.withColumn(
"Partition",
when(($"RelationObjectId_relatedObjectType" === "EDInstrument" && $"RelationObjectId_relatedObjectType" === "Fundamental"), "Instrument2Fundamental")
  .otherwise(
    when($"RelationObjectId_relatedObjectType" === "EDInstrument" && $"RelationObjectId_relatedObjectType" === "FundamentalSeries", "Instrument2FundamentalSeries")
      .otherwise(
        when($"RelationObjectId_relatedObjectType" === "Organization" && $"RelationObjectId_relatedObjectType" === "Fundamental", "Organization2Fundamental")
          .otherwise(when($"RelationObjectId_relatedObjectType" === "Organization" && $"RelationObjectId_relatedObjectType" === "FundamentalSeries", "Organization2FundamentalSeries")
          )
      )
  )