On Mon, Feb 19, 2001 at 05:33:00PM -0500, Doug Linley wrote:
> I have a table that I created that has a primary key field that should be
> auto_incremented. To fill in this value I am calling
>
> insert into player_table(playerid, player_name) values (LAST_INSERT_ID() +
> 1, '$player_name');
>
> My problem is that I'm getting duplicated key values because last_insert_id
> is always 0. Anyone know what the problem might be? The field is indexed
> by the way.
>
MySQL assigns a new auto_increment value for you when you
don't specify one in your insert or when you specify NULL
in your insert.
LAST_INSERT_ID is used to find out which value MySQL chose
for you, so you can use it to insert a record in another
table that references this new record.
E.g. (excuse my PHP, I assume that's what this '$player_name'
is and I don't use PHP myself):
insert into player_table(playerid, player_name)
values (NULL, '$player_name');
insert into scores(playerid, game, score)
values (LAST_INSERT_ID(), 1, 147);
You however, do specify a value for the playerid:
LAST_INSERT_ID() + 1.
Since no auto_increment value has been assigned by MySQL,
LAST_INSERT_ID() is still 0 and will always be zero.
Hope this clears things up.
Regards,
Fred.
--
Fred van Engen XO Communications B.V.
email: [EMAIL PROTECTED] Televisieweg 2
tel: +31 36 5462400 1322 AC Almere
fax: +31 36 5462424 The Netherlands
---------------------------------------------------------------------
Before posting, please check:
http://www.mysql.com/manual.php (the manual)
http://lists.mysql.com/ (the list archive)
To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php