matt2kjones
11-30-2002, 03:18 PM
ok im gunna have a script where about 20 people are submitting data at the same time and i have a question about mysql_insert_id
does it show the last_insert_id for INSERT query just before it, or does it show the last ever INSERT query that went into the db
the reason i ask i want the last auto_increment number THAT particular users script made, not the last one ever made, because its important i get the number created by their insert query
so does it show that last ever insert number in a db, or does it show the last insert number made by the INSERT query that happened in THAT particular php page.
thanx
beowulfdk
11-30-2002, 06:50 PM
>>so does it show that last ever insert number in a db, or does it show the last insert number made by the INSERT query that happened in THAT particular php page.
Shows last auto increment value last time an auto increment was made. MySQL is like a server, I do not think that it keeps track of which php pages make which queries - it just serves it. For your problem you probably need to lock the database while you do the insert and get the increment value...
jtrovato
11-30-2002, 10:55 PM
SELECT MAX(id) from table
this will return the last inserted ID number into the table. One problem with this is that, if you have 20 people inserting like mad men and women, someone might gets theirs in at or around the same time, so it might cause the numbers to be off by one. A good way is to do a query right afterwards that looks for the data that was just entered, As long as you know the data is unique that was entered by the user. You could put a timmer in there from the server's clock to make sure that you are selecting the correct ID number.
John
GanonOfEvil
12-01-2002, 01:42 AM
This really shouldnt be a problem (you shouldnt get the last insert id and use it later, it should be retrieved the same time you are updating the database). last_insert_id() retrieves the last insert ID for a thread, not database or table. A thread is basically an instance of a connection/execution. I believe PHP executes all statements for one database/connection as 1 thread ,I may be wrong though. If it becomes a problem, you can always lock the table while you are using it. Last_insert_id()
http://www.mysql.com/doc/en/LOCK_TABLES.html
http://www.php.net/manual/en/function.mysql-insert-id.php
jtrovato
12-01-2002, 12:55 PM
that's a better way, I didn't know about that.