insertMulti()
The insertMulti()
method inserts multiple rows into the same table using a single query. It is ideal for batch operations where you also need the inserted auto-increment IDs.
Signature:
public function insertMulti(string $table, array $rows): array|false
Basic Usage
Each row must be an associative array with identical keys (columns):
$rows = [
[
'user_id' => 1,
'product_id' => 101,
'price' => 12.50,
'status' => 'completed',
'order_date' => $db->now()
],
[
'user_id' => 2,
'product_id' => 103,
'price' => 8.99,
'status' => 'pending',
'order_date' => $db->now()
]
];
$ids = $db->insertMulti('test_orders', $rows);
Return Value
array
with inserted IDs on success (one per row)false
on failure (checkgetLastError()
)$db->count
returns the number of inserted rows
Performance Tip:
If you don’t need the inserted IDs, consider using
If you don’t need the inserted IDs, consider using
insertBulk()
instead. It is significantly faster for large datasets.
Requirements
- All rows must use the same columns and order
- Use
$db->now()
or$db->func('SQL_FN')
for SQL-based values