On Tue, Nov 19, 2002 at 05:57:56PM -0500, Jeff Urlwin wrote: > > code snippet: > > > > use DBI; > > my $dbh = DBI->connect('DBI:ODBC:OST', 'user', 'pass') > > or die "Couldn't connect to database: " . DBI->errstr; > > > > sub get_max_id { > > my $be_table=@_[0]; > > my $st_maxid = $dbh->prepare("select max(OST_ID) from $be_table where > > event_status='U'"); > > $st_maxid->execute(); > > my $maxid = $st_maxid->fetchrow_array; > > $st_maxid->finish; > > You're getting an array back from fetchrow_array, not a scalar. > > my @row = $st_maxid->fetchrow_array; > my $maxid = defined(@row) ? $row[0] : undef; > > The above should work
Umm, defined() shouldn't be used on arrays - it doesn't mean what you expect. This would be okay: my $maxid = @row ? $row[0] : undef; but since $row[0] would be undef anyway if there are no elements in @row you could just write my $maxid = $row[0]; And since there's only one field being selected fetchrow_array will return that field when called in a scalar context, so you could just write: my $maxid = $st_maxid->fetchrow_array; But since the prepare+execute+fetch+finish are all in sequence you could replace them all with: my $maxid = $dbh->selectrow_array(qq{ SELECT max(OST_ID) FROM $be_table WHERE event_status='U' }); and, if you want to map undef (due to no rows matching) to something else, I'd do this: $maxid = 0 unless defined $maxid; It's important to remember that if you're _not_ using RaiseError then undef can be returned due to an error. So when no using RaiseError you should always add a test for "if ($DBI::err) { ... }". Tim.