<?php
function bulkInsert(PDO $db, string $table, array $rows): int {
if (!$rows) return 0;
$cols = array_keys($rows[0]);
$colList = '`' . implode('`,`', $cols) . '`';
$rowPh = '(' . implode(',', array_fill(0, count($cols), '?')) . ')';
$allPh = implode(',', array_fill(0, count($rows), $rowPh));
$sql = "INSERT INTO `$table` ($colList) VALUES $allPh";
$params = [];
foreach ($rows as $r) foreach ($cols as $c) $params[] = $r[$c] ?? null;
$stmt = $db->prepare($sql);
$stmt->execute($params);
return $stmt->rowCount();
}
bulkInsert($db, 'events', [
['user_id'=>1,'event'=>'login'],
['user_id'=>2,'event'=>'signup'],
['user_id'=>1,'event'=>'view_page'],
]);
Create a free account and build your private vault. Share publicly whenever you want.