<?php
function implode_flds(array $row){
$str = '';
$del = '';
foreach ($row as $f) {
if (is_null($f) or strtoupper($f) == 'NULL'){
$f = 'NULL';
} elseif (is_string($f)){
$f = '\'' . $f . '\'';
}
$str .= $del.$f;
$del = ',';
}
return $str;
}
$names = array_fill(0, 50000, array('foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo',
'lnlnlnlnlnlnlnlnlnlnlnlnfoofoofoofoofoofoofoofoofoofoo',
null,
'teltelteltelteltelteltelteltelteltelfoofoofoofoofoofoofoofoofoofoo',
'szszszszszszszszszszszszszszszszfoofoofoofoofoofoofoofoofoofoo',
'wghwghwghwghwghwghwghwwghwghwghwghwghwghwghwfoofoofoofoofoofoofoofoofoofoo'));
$start = microtime(true);
$insert = 'INSERT INTO `foo` (firstname, lastname, address, tel, size, weight) VALUES ';
foreach ($names as $name) {
$insert .= '('.implode_flds($name).'),';
}
$pdo->query(trim($insert, ','));
$duration = microtime(true) - $start;
echo 'Duration: '.$duration;
echo PHP_EOL;
$start = microtime(true);
$insert = 'INSERT INTO `foo` (firstname, lastname, address, tel, size, weight) VALUES ';
foreach($names as $name) $insert.= '(?,?,?,?,?,?),';
$names = array_merge(...$names);
$stmt = $pdo->prepare(trim($insert, ','));
$stmt->execute($names);
$duration = microtime(true) - $start;
echo 'Duration: '.$duration;
PHP version :
PHP 7.4
PHP 8.0
PHP 8.1
PHP 8.2
PHP 8.3
PHP 8.4
Run PHP Code
Save snippet