0

Possible Duplicate:
Retrieve inserted row ID in SQL

Is there any way to get inserted rows id right after the insertion without doing select query? Basically, the idea is like this, I have two tables, one products, and second storage which has 3 columns - id / productId / count . And I need to insert in productId the product Id of the product which I inserted right before this query. So for example -

Product with id 1.

I insert it into a table for example products, it generates id 1. I don't do select, but right after I do another insert which will insert in table storage these data -

id - 1 / productId - 1 (took from previous query which generated id 1 with AI) / count = 0.

Hope you understood what I ment.

EDIT: I'm using MYSQL database.

Community
  • 1
  • 1
y2ok
  • 660
  • 5
  • 18

2 Answers2

2

Yes, in MySQL, just use the LAST_INSERT_ID() in your second query.

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
  • Okay, gonna give it a try, hope it will work codeIgniter ;)! – y2ok Sep 15 '12 at 13:40
  • Edit: Your answer moved me to the correct place, in codeigniter it's very similar - $this->db->insert_id(); will provide last inserted row id. Thank you :) ! I'll accept ASAP. – y2ok Sep 15 '12 at 13:48
0

use mysql_insert_id(); this is a function of php it will return last inserted id of auto increment type attribute

StaticVariable
  • 5,253
  • 4
  • 23
  • 45