I have used $mysqli->insert_id;
which returned a single ID. How can I retrieve IDs for multiple inserted rows in mysqli
. Is there a solution?
Asked
Active
Viewed 49 times
0

Aaron
- 1
- 1
-
1Are you doing your INSERTs in a loop? If so, add the returned ID to an array on each iteration of the loop. – foxbeefly May 25 '15 at 08:57
-
If you are using loop for inserting data then you can make an array from there only easily – Narendrasingh Sisodia May 25 '15 at 09:08
-
@foxbeefly - No, I am not using loop for bulk insertion as I remember reading somewhere it slows down the performance. – Aaron May 25 '15 at 09:46
-
Then how are you inserting multiple records? Post code please... – foxbeefly May 25 '15 at 09:47
3 Answers
1
Use the following query:
SELECT * FROM myTable WHERE myColumn IN ($ids);
Please refer to this answer and its thread for more info on the subject.
EDIT: Misinterpreted the question. @DevLakshman has got the right idea.
-
Based on the inclusion of `$mysqli->insert_id;` I think @Aaron is trying to get the IDs of the records as they are inserted into the database, not return the records based on know IDs – foxbeefly May 25 '15 at 09:01
-
I just reread the question and I'm afraid I misinterpreted it. I will edit my answer. – D. Visser May 25 '15 at 09:02
0
Your next ids will be the succession of generated $mysqli->insert_id
+ the next n (number of inserts rows) as your statement is doing it in one transaction.

Afsar
- 3,104
- 2
- 25
- 35
-
what if there are multiple inserts (transactions) happening at the same time? – foxbeefly May 25 '15 at 09:03
0
If you are executing your insert query in loop then get the ids by $mysqli->insert_id;
by writing it in loop or if manually writing many queries then write $mysqli->insert_id;
correspondingly...

lakshman
- 656
- 4
- 18