How to make mysql_insert_id work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MyerInd
Forum Newbie
Posts: 5
Joined: Thu May 16, 2002 4:36 pm

How to make mysql_insert_id work

Post by MyerInd »

I am inserting data into one table where an auto_increment id is formed. I want to place this same id into a second table. I have tried using mysql_insert_id and last_insert_id but always get a "0". The first table is incrementing as it should, but I can't seem to capture this value.

Any thoughts?

I have read and tried suggestions from mysql and php.


Ron
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

php.net wrote:mysql_insert_id() returns 0 if the previous query does not generate an AUTO_INCREMENT value. If you need to save the value for later, be sure to call mysql_insert_id() immediately after the query that generates the value.
Are you calling the function directly after the query because even a line or two of code inbetween may stop it working.

Mac
Post Reply