When inserting data into a table which has an auto-incremented PK, I need to obtain that key for use in another statement. As many questions show on SO this can be done in PHP using mysql_insert_id()
.
However, I have been grouping my inserts together so I insert more than one row at a time. I did this as I guessed there would probably be some performance issue, please advise if I am wrong. Anyway, as far as I am aware however, mysql_insert_id()
only returns the last id, when I need the ids for all the inserted rows.
I guess in this case I could:
Do some simple maths to calculate all the ids using mysql_insert_id()
and the number of rows I have entered. But is this guaranteed be consistently correct?
Use multiple insert statements, one for each row
Generate my IDs before and no use auto-increment.
I am sure this must be a classic problem, so I am wondering on what the most common and advisable approaches are.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…