[Fwd: RE: Last_Insert_ID]

[Fwd: RE: Last_Insert_ID]

am 21.06.2004 12:54:54 von Jochen Wiedmann

-------- Original Message --------
Subject: RE: Last_Insert_ID
Date: Mon, 21 Jun 2004 11:36:59 +0100
From: Scott Simpson
To: 'Jochen Wiedmann'

Dear All,

For info. After some effort I seem to have resolved my problem. The
problem seems to be that SELECT LAST_INSERT_ID() returns an array! If you
expect that behaviour you can extract the last insert id from the list. If
on the other hand you expect a string you get the number of elements in the
array (i.e. the number of records in the table) which will be less than the
last insert id if you have deleted records.

The before and after code are listed below for posterity.

Regards,
Scott

The original code was:

sub c_new
{
my ($dbh, $params) = @_;
my $query = qq{INSERT INTO t_masts (f_site, f_name) VALUES
('$params->{f_site}', '$params->{f_name}')};
my $sth = $dbh->prepare(qq{$query});
$sth->execute();
$query = qq{SELECT LAST_INSERT_ID() from t_masts};
$sth = $dbh->prepare(qq{$query});
my $last_id = $sth->execute();
$sth->finish();
return $last_id;
}

Which has changed to:

sub c_new
{
my ($dbh, $params) = @_;
my $query = qq{INSERT INTO t_sites (f_customer, f_name,
f_description) VALUES ('$params->{f_customer}', '$params->{f_name}',
'$params->{f_description}')};
print qq{new: query = $query
};
my $sth = $dbh->prepare(qq{$query});
$sth->execute();
$sth->finish();
$query = qq{SELECT LAST_INSERT_ID() from t_sites};
$sth = $dbh->prepare(qq{$query});
$sth->execute();
my @ary = $sth->fetchrow_array();
my $last_site = $ary[0];
$sth->finish();
}



-----Original Message-----
From: Jochen Wiedmann [mailto:jochen.wiedmann@freenet.de]
Sent: 14 June 2004 13:23
To: Scott Simpson
Subject: RE: Last_Insert_ID

On Mo, 2004-06-14 at 13:29, Scott Simpson wrote:
> No threads in use. DBD driver is version 2.0416.

Try a current (see authors/R/RU/RUDY directory on CPAN). If the problem
still persists, contact the mailing list again.


Jochen



--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/perl?unsub=gcdmp-msql-mysql-modules@m .gmane.org