PDA

View Full Version : Syntax to retrieve an auto incremented id


Nomad
03-25-2004, 05:43 AM
Hi,

Could someone please tell me the correct syntax to retrieve an auto incremented id after an insert in PERL.

The code I have is :-

# Connect to the database
my $dbh = DBI->connect("DBI:mysql:database=$config->
{dbname};host=localhost", "$config->{dbuser}",
"$config->{dbpass}", {'RaiseError' => 1});

# Insert member record
$dbh->do("INSERT INTO members (password,email,etc....)
VALUES ('$encpass','$email',etc....)");

$uid = $sth->{insertid}; ?????????????

# Disconnect from database
$dbh->disconnect();

but I do not get anything back. I have also tried :-

SELECT LAST_INSER_ID();

but keep getting back "0".

Help please

Steveo31
03-25-2004, 05:52 AM
Oooh.. Perl.. not sure. In mySQL I have something like this:

CREATE TABLE `foo` (
`autoInc` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY ( `autoInc` )
);

Then get that `autoInc` back by:

SELECT `autoInc` FROM `foo`;

Nomad
03-25-2004, 05:59 AM
That would get me back everything on the table.

I could use SELECT MAX(userid)..... but that would give me the wrong answer if another session inserted a record between this INSERT and subsequent SELECT.

There is a way in perl to get back the last id created by this session, but I can't seem to get the syntax right.