目前我正在构建类似于此的PDO插页:
foreach($r->occurrences as $row) {
$arr[] = array(
'id' => $id,
'name' => $name,
'start' => $row->getTimestamp(),
'end' => $row->getTimestamp() + $duration,
'note' => $notes
);
};
$keys = implode(',', array_keys($arr[0]));
// Format into ('1','2','4'),('8','3','4') for insertion
foreach ($repBkgArr as &$array) {
$array = "('".implode("','", $array)."')";
}
$values = implode(',', $arr);
$query = "INSERT INTO table ($keys) VALUES $values";
$db->execute($query);
在这种情况下,我显然只能绑定执行函数的第二个参数中的变量,因为我批量插入了几组数据。
$db->execute($query, array(':id' => $id, ':name' => $name);
我该如何处理?我一整天都在研究,似乎无法找到任何我能适用于我情况的东西。
答案 0 :(得分:1)
最简单的方法:只需为每个绑定值添加索引前缀,如下所示:
$sql = 'INSERT INTO `tabke`(id, name) VALUES ';
$bind = array();
$sqlRows = array();
foreach ($r->occurrences as $index => $row) {
$sqlRows[] = "(:id_$index, :name_$index)";
$bind[':id_'.$index] = $row['id'];
$bind[':name_'.$index] = $row['name'];
}
print_r($sql.implode(',', $sqlRows)); // QUERY SQL
print_r($bind); // QUERY PARAMS