From 54dd165917b085bd13863e1f765cdaa2a26af447 Mon Sep 17 00:00:00 2001 From: Louis Rossouw Date: Thu, 16 Jul 2015 00:15:27 +0200 Subject: [PATCH] Create dbBulkInsert which allows bulk insertion of records. --- includes/dbFacile.php | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/includes/dbFacile.php b/includes/dbFacile.php index ca661def0a..90d7c67622 100644 --- a/includes/dbFacile.php +++ b/includes/dbFacile.php @@ -108,6 +108,63 @@ function dbInsert($data, $table) { }//end dbInsert() +/* + * Passed an array and a table name, it attempts to insert the data into the table. + * $data is an array (rows) of key value pairs. keys are fields. Rows need to have same fields. + * Check for boolean false to determine whether insert failed + * */ + + +function dbBulkInsert($data, $table) { + global $fullSql; + global $db_stats; + // the following block swaps the parameters if they were given in the wrong order. + // it allows the method to work for those that would rather it (or expect it to) + // follow closer with SQL convention: + // insert into the TABLE this DATA + if (is_string($data) && is_array($table)) { + $tmp = $data; + $data = $table; + $table = $tmp; + // trigger_error('QDB - Parameters passed to insert() were in reverse order, but it has been allowed', E_USER_NOTICE); + } + if (count($data) === 0) { + return false; + } + if (count($data[0]) === 0) { + return false; + } + + $sql = 'INSERT INTO `'.$table.'` (`'.implode('`,`', array_keys($data[0])).'`) VALUES '; + $values =''; + + foreach ($data as $row) { + if ($values != '') { + $values .= ','; + } + $rowvalues=''; + foreach ($row as $key => $value) { + if ($rowvalues != '') { + $rowvalues .= ','; + } + $rowvalues .= "'".mres($value)."'"; + } + $values .= "(".$rowvalues.")"; + } + + $time_start = microtime(true); + $result = dbQuery($sql.$values); + + // logfile($fullSql); + $time_end = microtime(true); + $db_stats['insert_sec'] += number_format(($time_end - $time_start), 8); + $db_stats['insert']++; + + return $id; + +}//end dbBulkInsert() + + /* * Passed an array, table name, WHERE clause, and placeholder parameters, it attempts to update a record. * Returns the number of affected rows