Reputation: 441
I am writing a Perl script that uses DBI to connect to a MySQL database. I am trying to fetch
a column that is the value of a multiplication SUM(amount, 365)
in MySQL the code works, and also in Perl the code works for SUM(amount)
My thoughts are that the multiplication is putting out a different type of value that I cannot read through my method. I could be off though, still pretty new to Perl.
Here is code
my ($sum, $sum365);
$sth = $dbh->prepare ("
SELECT SUM(amount), SUM(amount) * 365)
FROM spending
");
$sth->execute()
or die "cannot execute\n";
$sth->bind_col(1, \$sum);
$sth->bind_col(2, \$sum365);
while ( $sth->fetch ){
print "$sum\n";
print "$sum365\n";
}
Another work around I am thinking of, is there a way to have Perl read the value as numeric? Then I could just create another variable $sum365 = $sum * 365
. I tried that too, but Perl is not reading the value as numeric.
Also I am aware there are different methods in Perl to fetch
from database array
, arrayref
etc. Is there another method that would work, and why?
Upvotes: 2
Views: 117
Reputation: 311448
You have a redundant closing bracket ()
) in your query after "365":
$sth = $dbh->prepare ("
SELECT SUM(amount), SUM(amount) * 365
FROM spending
");
Upvotes: 1