Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy way to get an auto-incremented ID when inserting new row? 1

Status
Not open for further replies.

dkdude

Programmer
Jun 16, 2003
849
DK
Hi,

When adding a new row to a table in PHP, is there an easy way to obtain the assigned auto-incremented field without "looking up" the field again?

As you've guessed, the table cantains an ID field that is auto-incremented and I create the new table row with
Code:
INSERT INTO
...

Thanks §:O)


Jakob
 
you must leave out the autoincrement keyfield in you're insert statement. then it will work automaticaly

so not "insert into table (keyfield,otherfield) values ('10','test') "

but
"insert into table (otherfield) values ('test') "
 
hos2,

Thanks for your prompt reply!

I am aware of what you say. However, I need the ID field in a step by step set of forms and were looking for a way to get the (auto-incremented) ID of the row created in the first form-step -but without looking up the row again in the DB.

Do you see where I'm going?

Now I've got the following steps:

Code:
createNewRow($dataArray);
$id = getRowID($dataArray);

That's two DB calls -can it be done with only one? So my code would look like:

Code:
$id = createNewRow($dataArray);

And contain only one MySQL call?!

Many Thanks §:O)


Jakob
 
You can use mysql_insert_id() right after insert.

________
George, M
 
yep

$keyvalue=mysql_insert_id(); gives you what you need.

I also use that functions when I create orders with orderlines in one step

 
Thank you both! I'll give it a try tomorrow...

Sounds like it is exactly the function I'm looking for §:O)

Best Regards

dkdude1.gif



Jakob
 
George,

Excellent! That looks very good! I'll have a look to see if I can figure it out!

Many Thanks


Jakob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top