简体   繁体   English

PHP -MySQL insert和select语句-如何使其原子化?

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

Im new to PHP and I was wondering how I can overcome this seemingly simple problem: 我是PHP的新手,我想知道如何克服这个看似简单的问题:

I have a database with several tables. 我有一个带有多个表的数据库。 Of them 1 table is called "order_header". 其中1个表称为“ order_header”。 Order header has a field called "orderID" which is the primaryKey and is auto-incremented. 订单标头具有一个称为“ orderID”的字段,该字段是primaryKey,并且会自动递增。 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. OrderID在数据库的其他表(food_table,drinks_table,merchant_info,customer_info等)中使用,并且对于特定订单是唯一的。

Now I insert data into the order_header using the usual INSERT statement and the order_header generates a new orderID. 现在,我使用通常的INSERT语句将数据插入order_header中,并且order_header生成一个新的orderID。 But now I need to retrieve the orderID I just created and use it to insert data into other tables of that database. 但是现在,我需要检索刚刚创建的orderID,并使用它将数据插入到该数据库的其他表中。

The question is how can I do both inserting data and retrieving the resulting orderID in one atomic method? 问题是,如何在一种原子方法中插入数据并检索得到的orderID? 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. 我不能使用mySQL查询来获取最后的orderID,因为如果同时有另一个线程在orderID中插入了一个条目,该怎么办。

In Java I guess one could use locks and the word @synchronized, but how would one do this in PHP? 在Java中,我猜一个人可以使用锁和@synchronized这个词,但是在PHP中怎么做呢?

Use mysql_insert_id straight after the query. 查询后立即使用mysql_insert_id It doesn't run another query to find the last ID 它不会运行另一个查询来找到最后一个ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM