Created
August 11, 2013 15:13
-
-
Save omerucel/6205319 to your computer and use it in GitHub Desktop.
a function to generate bulk insert sql query for PDO.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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