Gossamer Forum
Home : General : Databases and SQL :

Mysql auto_increment

Quote Reply
Mysql auto_increment
Hello,

I'm having trouble getting information on tables in mysql,

I can't retrieve the number next in line for a field set to auto_increment.

I'm using LAST_INSERT_ID() in this fashion...

$sth = $dbh->do(<<End_SQL) or print "Couldn't prepare statement: $DBI::errstr; stopped";
INSERT INTO table1 VALUES (NULL,'Field1')
End_SQL


$sth = $dbh->do(<<End_SQL) or print "Couldn't prepare statement: $DBI::errstr; stopped";
INSERT INTO table2 VALUES ('',LAST_INSERT_ID(),'field1')
End_SQL

But I can't seem to get the generated number for field one into a $variable.

I shouldn't have to add a new entry into the table just to get the next number should I?

I can barely get the number of records with:

$sth = $dbh->do(<<End_SQL) or print "Couldn't prepare statement: $DBI::errstr; stopped";
SELECT * FROM table
End_SQL

$rows=$sth;



I'm having difficulty getting very simple information from these tables and have read the docs up & down.

Can someone help me put the peices together?

Thank you,
Tony
Quote Reply
Re: [clearfox] Mysql auto_increment In reply to
Quote:
I can't retrieve the number next in line for a field set to auto_increment.

I'm not quite sure what your trying to do here. I'm assuming you are trying to grab the last ID number entered into the MySQL table...but why? auto_increment does this for you. Simply leave its value blank when doing an INSERT.

Hope that helps.

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [clearfox] Mysql auto_increment In reply to
You can get it with:

my $id = $dbh->{insert_id};
Quote Reply
Re: [Paul] Mysql auto_increment In reply to
And then you'll need:

my $next_number = $id + 1;

- wil
Quote Reply
Re: [Wil] Mysql auto_increment In reply to
Or just:

my $id = $dbh->{insert_id} + 1;

Last edited by:

Paul: May 24, 2002, 3:31 AM
Quote Reply
Re: [Wil] Mysql auto_increment In reply to
Infact looking at the question, he seems to be wanting to insert the last id into table2 so you don't need to add one.
Quote Reply
Re: [Paul] Mysql auto_increment In reply to
I was just going on the line that Andy quoted.

How's the exams going?

- wil
Quote Reply
Re: [Wil] Mysql auto_increment In reply to
I don't have any. Just got bored with the forum this week so had a few days away and played a few golf competitions with a friend....and won Wink

Im just about to make a trip to sainsburys and then watch the Volvo PGA Championship on tv.

Im playing on wednesday too....gotta get my handicap down from 5.4....I've been stuck there for a while.

Last edited by:

Paul: May 24, 2002, 4:44 AM
Quote Reply
Re: [Paul] Mysql auto_increment In reply to
hey paul,

NetscapeŽ Communicator 4.79 from the stoneage?

Unimpressed

regan.
Quote Reply
Re: [ryel01] Mysql auto_increment In reply to
If you mean WiredON then yep:

if (browser.match(/4\.[67]/)) {

Last edited by:

Paul: May 26, 2002, 4:41 AM
Quote Reply
Re: [Paul] Mysql auto_increment In reply to
THank you very much paul, I couldn't ever make my way back here and then I found my email confirmations. So I just saw it today.

Thanks a whole lot, especially since I was just about to ask again becuase I needed that line of code so bad.

Thanks
Quote Reply
Re: [clearfox] Mysql auto_increment In reply to
Actually I just needed to get the id of an auto_increment column for a script I was writing. I used:

$self->{dbh}->{mysql_insertid};

...so you'd probably need:

$dbh->{mysql_insertid};