What are the advantages of using Multiple-Insert statements in PHP when dealing with large amounts of data?

When dealing with large amounts of data in PHP, using Multiple-Insert statements can significantly improve performance compared to executing individual INSERT queries for each row of data. This is because Multiple-Insert statements allow you to insert multiple rows of data in a single query, reducing the number of round-trips to the database server and improving overall efficiency.

// Example of using Multiple-Insert statements in PHP
$data = [
    ['John', 25],
    ['Jane', 30],
    ['Alice', 22],
    ['Bob', 28]
];

// Connect to database
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');

// Prepare the Multiple-Insert query
$query = "INSERT INTO users (name, age) VALUES ";
$values = [];
$placeholders = [];

foreach ($data as $row) {
    $values = '(' . $pdo->quote($row[0]) . ', ' . $pdo->quote($row[1]) . ')';
    $placeholders[] = $values;
}

$query .= implode(',', $placeholders);

// Execute the Multiple-Insert query
$pdo->exec($query);