Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
CREATE TABLE table_name (
id INT AUTO_INCREMENT,
column_1 INT,
column_2 INT,
PRIMARY KEY (id)
);
<?php
$data_list = [
['specific_key' => 1, 'another_key' => 2],
['specific_key' => 3, 'another_key' => 4],
['specific_key' => 5, 'another_key' => 6],
];
$values = [];
$placeholders = [];
foreach ($data_list as $row) {
$placeholders[] = '(?,?)';
array_push($values, $row['specific_key'], $row['another_key']);
}
if ($values) {
$pdo->prepare(
sprintf(
'INSERT INTO `table_name` (`column_1`,`column_2`) VALUES %s',
implode(',', $placeholders)
)
)->execute($values);
}
var_export(
$pdo->query("SELECT * FROM table_name")->fetchAll(PDO::FETCH_ASSOC)
);