Sorry how do i check $DBI::err in this case?
This is the entire snippet of code that I"m calling:
use huff::WelcomeMessage;
my $driver = MT::Entry->driver;
my $dbh = $driver->{dbh};
my $query = "SELECT Name from NGBlogWelcome";
my $welcome = pop(@{$dbh->selectcol_arrayref($query)});
$param{welcome} = $welcome;
| [reply] [d/l] |
if (my $ref = $dbh->selectcol_arrayref($query)) {
$param{welcome} = pop @$ref;
}
elsif ($DBI::err) {
die $dbh->errstr;
}
else {
# nothing returned, but no error?
}
update: you did notice that selectcol_arrayref returns ALL values in that column, right? I ask because using it to just get the very first row's value will get extremely inefficient fast.
| [reply] [d/l] |
An alternative to Joost's solution is to set the RaiseError flag to true, causing DBI to throw an exception instead of retuning undef. However, avoiding RaiseError allows you to provide a more meaningful error message.
For example, the following dies with a meaninful error message on error. It also only reads one row since that's all you want.
my $ref = $dbh->selectcol_arrayref($query)
or die("Unable to fetch blog name: ",
$DBI::err || "Unknown database error",
"\n");
$param{welcome} = $ref->[0];
Update: Added example.
| [reply] [d/l] [select] |