insertBulk()

The insertBulk() method inserts multiple rows into a table using a single, highly optimized INSERT INTO ... VALUES (...), (...) query. It is ideal for large datasets where performance is more important than retrieving inserted IDs.

Signature:

public function insertBulk(string $table, array $rows): bool

Basic Usage

Each row must be an associative array with identical keys (column names and order):

$rows = [
  [
    'user_id'     => 1,
    'product_id'  => 101,
    'price'       => 12.50,
    'status'      => 'completed',
    'order_date'  => '2025-07-19 18:00:00'
  ],
  [
    'user_id'     => 2,
    'product_id'  => 103,
    'price'       => 8.99,
    'status'      => 'pending',
    'order_date'  => '2025-07-19 18:01:00'
  ]
];

$success = $db->insertBulk('test_orders', $rows);

Return Value

  • true on success
  • false on failure (check getLastError())
  • $db->count returns the number of inserted rows

Requirements

  • All rows must use the same column names and order
  • Only scalar values are supported – no SQL expressions or subqueries
  • The target table must allow multi-row INSERT statements

See Also