mysql_insert_id

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
qdiggityd
Forum Newbie
Posts: 3
Joined: Thu Jul 11, 2002 1:29 pm
Contact:

mysql_insert_id

Post by qdiggityd »

howdy all,

im trying to configure a php forum to work with a portal system I built. The problem I am having is when a user replies to a post, it inserts the post data into the posts table then it will grab the last autonumber id, which is the postifd, by using the mysql_insert_id function and insert it into other tables such as the posts_text table. The problem is, and I don't know how this happened, but the autonumber will generate a number like 6, but the mysql_insert_id will return a number like 2453. Maybe Im just missing something but I know how I can get around the problem - just by quering the database for the newest postid and get it that way, but it isnt as efficient. Any help would be greatly appreciated. Thanks
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

Is the postifd field BIGINT? If so then that may be the problem. Try using the SQL function LAST_INSERT_ID() to set the ID based off of your postifd. Insert the posts row, and then use LAST_INSERT_ID() to set the id for the new rows in the other table, ex:

Code: Select all

INSERT INTO posts_text (id, otherfield) VALUES (LAST_INSERT_ID(), 'foo')
qdiggityd
Forum Newbie
Posts: 3
Joined: Thu Jul 11, 2002 1:29 pm
Contact:

Post by qdiggityd »

works like a charm......thanks
Post Reply