1. Go to this page and download the library: Download jotagp/insert-multiple library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jotagp / insert-multiple example snippets
// example with tradicional insert
foreach ($list as $item) {
$insert = "INSERT INTO `table1`(`numbers`, `description`) VALUES ({$item['number']}, '{$item['description']}');
$connection->query($insert) or die ($connection->error);
}
// example with tradicional insert and transaction control
$connection->begin_transaction();
foreach ($list as $item) {
$insert = "INSERT INTO `table1`(`numbers`, `description`) VALUES ({$item['number']}, '{$item['description']}');
$connection->query($insert) or die ($connection->error);
}
$connection->commit();
// example with manually multiple insert
$connection->begin_transaction();
$insert = "INSERT INTO `table1`(`numbers`, `description`) VALUES ";
foreach ($list as $item) {
$values[] = "({$item['number']}, '{$item['description']}')"; // concat new values
}
$insert .= implode(", ", $values); // join values separete by comma
$connection->query($insert) or die ($connection->error);
$connection->commit();
// example with package
$insert = new insert_multiple($connection, "table1");
foreach ($list as $item) {
$insert->push($item); // concat new values
}
$insert->exec(); // run inserts
use jotagp\insert_multiple\insert_multiple;
$connection = new mysqli('host', 'user', 'pass', 'database');
$insert_multiple = new insert_multiple($connection, 'table-name');
otagp\insert_multiple\insert_multiple;
$connection = new mysqli('localhost','root', 'root', '0temp') or die($connection->error);
$insert_multiple = new insert_multiple($connection, 'table1');
// generate randon numbers, between 1 and 1000
$count = 0;
while ($count < 100000) {
$list[] = ['value' => rand(1, 1000)];
$count += 1;
}
// insert multiple
echo "\nStart at: ". Date('Y-m-d H:i:s') . "\n[ ";
$count = 0; // for print a simple log
foreach ($list as $key => $item) {
$table1 = []; // clean the associative array
$table1['number'] = $key; // the 'number' index, matches the database attribute
$table1['descriptions'] = $item['value']; // the 'number' index, matches the database attribute
// magic is here
$insert_multiple->push($table1); // push the associative array
$count += 1;
if ($count % 1000 == 0) echo "#";
if ($count % 10000 == 0) echo " ". $count ." ";
}
// magic is here two
$insert_multiple->exec();
// update multiple
$update_multiple = new insert_multiple($connection, 'table1');
$update_multiple->config([
'update_if_exists' => [
'fields_to_update' => [
'description'
]
// 'concat_new_values' => true,
// 'skip_if_already_exists' => true,
// 'skip_if_new_is_empty' => true
]
]);
echo "\nStart at: ". Date('Y-m-d H:i:s') . "\n[ ";
$count = 0; // for print a simple log
foreach ($list as $key => $item) {
$table1 = [];
$table1['number'] = $key; // important! when you go update a row, remember set the key property (in this case, field number)
$table1['descriptions'] = 'data updated by jotagp lib';
$update_multiple->push($table1);
$count += 1;
if ($count % 1000 == 0) echo "#";
if ($count % 10000 == 0) echo " ". $count ." ";
}
$update_multiple->exec();
echo "]\nfinished at: ". Date('Y-m-d H:i:s') . "\n";
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.