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 successfalse
on failure (checkgetLastError()
)$db->count
returns the number of inserted rows
Note:
This method does not support SQL functions (like
This method does not support SQL functions (like
[F] => NOW()
) or subqueries. Only scalar values (int, float, string, null) are allowed.
Use Case:
If you need the inserted auto-increment IDs or want to use SQL expressions, use
If you need the inserted auto-increment IDs or want to use SQL expressions, use
insertMulti()
instead.
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