Forum Moderators: coopster

Message Too Old, No Replies

MySQL query

How to return the value of the auto increment

         

eggy ricardo

4:26 pm on Nov 25, 2004 (gmt 0)

10+ Year Member



Hi guys

I am developing what could be likened to a very simple php/mysql forum solution for a client.

I have come to an instance where I need to do two INSERT queries on my MySQL database to put data into two tables. However i need to put the value of one field in the first INSERT into the second as a foreign key sorta thing.

The field i need the value for is an auto incrementing field. Is it possible to return the value of the auto incremented field after and INSERT query so that i can then use this value and insert it into the second table.
I think I could probably do this by doing a SELECT query in between to find the max value of that column and then use that but I can't see this being the most efficient method.

Please can you suggest how to do this.

Many thanks
Richard

paladin

5:33 pm on Nov 25, 2004 (gmt 0)

10+ Year Member



The problem you may enounter is if you have more then one person posting at the same time.

Imagine you have user A & B posting. A does the foirst part and immediatley after user B. The scripe for user A then goes to get the latest value in the DB, only to find that it belongs to user B.

That is where mysql_insert_id come in. You can find more info about it at [dev.mysql.com...]

Salsa

5:41 pm on Nov 25, 2004 (gmt 0)

10+ Year Member



...and php.net documentation is at mysql_insert_id() [us2.php.net]

eggy ricardo

9:58 pm on Nov 25, 2004 (gmt 0)

10+ Year Member



Hiya

"The problem you may enounter is if you have more then one person posting at the same time."

Yea thats precisely the reason I posted because I was thinking that this may be a problem.

I'll look into those links.

Thanks guys