What are the benefits of using a while loop in PHP for inserting multiple entries into a database table?

When inserting multiple entries into a database table in PHP, using a while loop can efficiently iterate through a list of data and insert each entry one by one. This approach reduces the need for repetitive code and streamlines the insertion process.

<?php
// Sample array of data to be inserted into the database
$data = array(
    array('John', 'Doe'),
    array('Jane', 'Smith'),
    array('Alice', 'Johnson')
);

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

// Prepare the SQL statement
$stmt = $pdo->prepare("INSERT INTO users (first_name, last_name) VALUES (?, ?)");

// Iterate through the data array and insert each entry into the database
foreach ($data as $entry) {
    $stmt->execute($entry);
}

echo "Data inserted successfully";
?>