您至少有以下两个选择:
$rows = [(1,2,3), (4,5,6), (7,8,9) ... ];$sql = "insert into `table_name` (col1, col2, col3) values (?,?,?)";$stmt = $db->prepare($sql);foreach($rows as $row){ $stmt->execute($row);}OR:$rows = [(1,2,3), (4,5,6), (7,8,9) ... ];$sql = "insert into `table_name` (col1, col2, col3) values ";$paramArray = array();$sqlArray = array();foreach($rows as $row){ $sqlArray[] = '(' . implode(',', array_fill(0, count($row), '?')) . ')'; foreach($row as $element) { $paramArray[] = $element; }}// $sqlArray will look like: ["(?,?,?)", "(?,?,?)", ... ]// Your $paramArray will basically be a flattened version of $rows.$sql .= implode(',', $sqlArray);$stmt = $db->prepare($sql);$stmt->execute($paramArray);如您所见,第一个版本具有许多简单的代码;但是第二个版本确实执行了批量插入。批处理插入应该更快,但是我同意 @BillKarwin的观点
,在绝大多数实现中性能差异不会被注意到。



