开发者

PHP -MySQL insert and select statements - how to make them atomic?

开发者 https://www.devze.com 2023-03-26 07:24 出处:网络
Im new to PHP and I was wondering how I can overcome this seemingly simple problem: I have a database with several tables. Of them 1 table is called \"order_header\". Order header has a field called

Im new to PHP and I was wondering how I can overcome this seemingly simple problem:

I have a database with several tables. Of them 1 table is called "order_header". Order header has a field called "orderID" which is the primaryKey and is auto-incremented. OrderID is used in other tables in the database (food_table, drinks_table, merchant_info, customer_info, etc)and is unique to a particular order.

Now I insert data into the order_header using the usual INSERT statement and the order_header generates a new orderID. But now I need to retrieve the orderID I just created and use it to insert data into other t开发者_开发知识库ables of that database.

The question is how can I do both inserting data and retrieving the resulting orderID in one atomic method? I cannot use the mySQL query to get the last orderID because what if another thread has inserted an entry in orderID in the meanwhile.

In Java I guess one could use locks and the word @synchronized, but how would one do this in PHP?


Use mysql_insert_id straight after the query. It doesn't run another query to find the last ID

0

精彩评论

暂无评论...
验证码 换一张
取 消