Using mysql_insert_id to insert a lot of data

I want to use PHP and MySQL to insert data into a data table and its many-to-many table. Automatically increasing the primary data table identifier is used in many tables. So, I need to know the identifier of the data table to insert into the many-to-many table.

My current thinking is that the best way to approach this is with a loop that will first be inserted into the data table and then use mysql_insert_id () to insert into the many-to-many table. Is this the best way to do this?

Also, do I need to worry about getting the wrong id with this function? Many workers will use this script data at the same time, so I am concerned that the identifier from one work request will be returned in the mysql_insert_id () call of the other. Is this a serious problem, and if so, how can I deal with it?

+3
source share
2 answers

Calling mysql_insert_id()in a table with the identifier auto_increment is exactly the right approach.

As long as you call mysql_insert_id()right after the call mysql_query()to insert a row, you don’t have to worry about getting the wrong identifier. PHP and MySQL take care of all this for you.

+3

.

LOCK TABLE myTable FOR WRITE;
SELECT MAX(id) FROM myTable;
INSERT....
INSERT....
INSERT....
SELECT MAX(id) FROM myTable;
UNLOCK TABLES;

- - myTable, min/max .

-daniel

-3

Source: https://habr.com/ru/post/1764788/


All Articles