Skip to content

Instantly share code, notes, and snippets.

@omerucel
Created August 11, 2013 15:13
Show Gist options
  • Save omerucel/6205319 to your computer and use it in GitHub Desktop.
Save omerucel/6205319 to your computer and use it in GitHub Desktop.
a function to generate bulk insert sql query for PDO.
<?php
function createBulkInsertSql($sql, $rowCount, $columnSize = 1) {
$rowCount = intval($rowCount);
$columnSize = intval($columnSize);
$columnMarker = array_fill(0, $columnSize, '?');
$rowValues = array_fill(0, $rowCount, '(' . implode(',', $columnMarker) . ')');
$sql .= ' VALUES' . implode(',', $rowValues);
return $sql;
}
$sql = 'INSERT INTO table(column1, column2)';
$data = array(
'column1.a', 'column2.a',
'column1.b', 'column2.b',
'column1.c', 'column2.c'
);
echo createBulkInsertSql($sql, 10, 2);
// INSERT INTO table(column1, column2) VALUES(?,?),(?,?),(?,?),(?,?),(?,?),(?,?),(?,?),(?,?),(?,?),(?,?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment