I've been using PDO and the following function to insert multiple records in chunks of 1000 at once. Now I'm working with a system that is using mysqli and I was wondering if I could slightly modify my function to work with mysqli as well however I noticed that mysqli execute doesn't accept an array as a parameter. The following function works perfectly fine and fast with PDO:
$sub_data = array_chunk($data, 1000);
for ($b = 0; $b < count($sub_data); $b++)
{
$insert_values = array();
for ($a = 0; $a < count($sub_data[$b]); $a++)
{
$insert_values = array_merge($insert_values, array_values($sub_data[$b][$a]));
$placeholder[] = '(' . implode(', ', array_fill(0, count($sub_data[$b][$a]), '?')) . ')';
}
$sql2 = "INSERT INTO $table_name (" . implode(",", array_keys($sub_data[$b][0])) . ") VALUES " . implode(',', $placeholder) . "";
$prepare = $db->prepare($sql2);
try
{
$prepare->execute($insert_values);
}
catch (mysqli_sql_exception $e)
{
echo "<pre>";
print_r($sub_data[$b]);
echo "</pre>";
echo $e->getMessage();
print_r($db->errorInfo());
}
unset($insert_values);
unset($placeholder);
}
Thank you!