title: Retrieving the last insert id from MySql using PHP
body: <p>I have two tables in MySql database, one is <code>sales_order</code> and the other is <code>sales_order_details</code> which contains the details of the order in the <code>sales_order</code> table. Obviously, there is a one to many relationship from <code>sales_order</code> to <code>sales_order_details</code>.</p>
  
  <p>When a customer places an order, the first entry is made into the <code>sales_order</code> table and based on the <code>auto_increment</code> id of the <code>sales_order</code> table, the corresponding entry is made into the <code>sales_order_details</code> table.</p>
  
  <p>I'm using the <code>last_insert_id()</code> MySql function to retrieve the corresponding <code>order_id</code> from the <code>sales_order</code> table, Something like this.</p>
  
  <pre><code>insert into sales_order_details(order_id, prod_id, prod_price)values(last_insert_id(), 5, 1500);
  </code></pre>
  
  <p>It's working. The <code>last_insert_id()</code> function retrieves the last inserted id from the respective table which is unique to a particular connection (as far as I know).</p>
  
  <p>Now, I need the same <code>order_id</code> which is retrieved and inserted most recently by the <code>last_insert_id()</code> function to send it as an invoice number to a payment system.</p>
  
  <p>I can try using the PHP function <code>mysql_insert_id()</code> but I'm not sure whether it works as specified. What is the correct way to retrieve the last insert id which always guarantees to retrieve the specific id which is always associated with  a particular order?</p>