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


Problem :

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 tables 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?



Solution :

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


Mysql Tutorials

Mysql Howto..

MySQL How to select date field exactly 7 days before today and for time last 1 hour

How to select mysql rows based off time increment adjustments (vb mysql)

NodeJS: How to stop code execution till create operation to MySQL completes

How to store the file path of an indexed document in Apache Solr 5.1.0

How do I connect a desktop app to an hosted database

how to design Hospital RDBMS in MySQL?

how to get mysql query for AI?

how to get all details about a mysql table using c#?

How to sort inner aggregate of date_histogram aggregation in Elasticsearch?

How To Get The Difference of a Two Date Value in MySQL and the result is in month wise?

How to search php array for all items from mysql table?

How to check for duplicates in mysql table over multiple columns

How to get a list of MySQL views?

how to import data from mysql to Apache Hadoop HDFS installation.

How to make a non-Primary Key a unique valid in mysql? [closed]

How does MySQL handle storing ENUM value that doesn't exist?

How to add MySQL SUM() function to this query?

How can I avoid entering duplicate entries in my MySQL database?

MySQL: how to get the last record?

How to match password username with php mysql with login form

Does MySQL record how often it uses indices?

How to SELECT and sort it by parent-children in MySQL

how do I import this data file to a Mysql table

Fetch html markup from database and show in a div

How to store data to MySQL for array input

How to create a BOOLEAN column in MySql using Eclipselink

How to refresh a MySQL table without website interruption?

How to get number of rows that COULD have been affected by an UPDATE?

How to select rows with same ID sharing a group of values?

How do you configure MySQL to run on localhost port 3306?