I have a script that has several nested foreach loops to obtain data from a MySQL database, and then I'm using GD::Graph::bars to graph this data. I have got it working to where it prints bar graphs for each query, but not all of the data is plotting correctly. Here is my script:


use DBI;
use GD::Graph::bars;

# Connect to database
$dsn = "DBI:mysql:bugs:host=$hostort=3306";
$db = DBI->connect($dsn, $user, $password) or die "Can't connect to the DB: $DBI::errstr\n";

@status=('All Bucket','Unresolved Bucket','Open Bucket','Closed Bucket');
@severities=('critical','major','minor','cosmetic' ,'feature_request');
print "Content-type: text/html\n\n";
foreach $sostatus(@status) {
foreach $currentquery("1","bugs.bug_status='NEW' or bugs.bug_status='unconfirmed' or bugs.bug_status='assigned' or bugs.bug_status='reopened'","bugs.bug_status='NEW' or bugs.bug_status='unconfirmed' or bugs.bug_status='assigned' or bugs.bug_status='resolved' or bugs.bug_status='reopened'","bugs.bug_status='veri fied' or bugs.bug_status='closed'") {
foreach $platform(@platforms) {
foreach $soversions(@versions) {
foreach $soseverities(@severities) {
$sql="select (bugs.bug_id) from bugs where (bugs.product='Sonata2' and bugs.bug_severity='$soseverities' and bugs.version='$soversions' and bugs.rep_platform='$platform' and ($currentquery))";
while (($bugid)=$sth->fetchrow_array()) {
$qscount="select bugs_customfields.value from bugs_customfields where cf_id='21' and bug_id='$bugid'";
if ($origvalue eq 'Test') {
} elsif ($origvalue eq 'EngTest') {
} elsif ($origvalue eq 'Development') {
} elsif ($origvalue eq 'Field') {
print "<p>Platform=$platform, Status=$sostatus, Severity=$soseverities, Version=$soversions</p>\n";
print "<p>Test=$testcount, EngTest=$engtestcount, Dev=$devcount, Field=$fieldcount, Total=$totalcount</p>\n";
@data = (["Test","EngTest","Dev","Field"],[$testcount,$engtestcount,$devcount,$fieldcount],[$totalcount,$totalcount,$totalcount,$totalcount]);
print "<p>Data contains the following:<br>$data[0][0],$data[0][1],$data[0][2],$data[0][3]<br>$data[1][0],$data[1][1],$data[1][2],$data[1][3]<br>$data[2][0],$data[2][1],$data[2][2],$data[2][3]</p>\n";
$bargraph = new GD::Graph::bars(400,500);
x_label => 'Bug Count',
y_label => 'Number of Bugs',
title => "$platform $sostatus $soseverities $soversions",
y_min_value => 0,
y_max_value => 30,
y_tick_number => 30,
y_label_skip => 2,
bar_spacing => 2,
transparent => 0,
$bargraph->set_legend('Number of Bugs', 'Total Number of Bugs');
$barimage = $bargraph->plot(\@data);
$outfile = join('',"/var/www/html/images/graphs/",$platform,$sostatus,$soseverities,$soversions,"b ar.png");
open(GRAPHBAR, ">$outfile") || die "Cannot open image file\n";
print GRAPHBAR $barimage->png;
$outimage = join('',"/images/graphs/",$platform,$sostatus,$soseverities,$soversions,"b ar.png");
print "<p><img src=\"$outimage\"></p>\n";

I have print statements to check the data going into the bar graph, and it is all correct, but sometimes I have data (like test=23, test=9), and they plot as 1 or 2, and I don't know why. I know this is really complex, but I can't really think of a way to simplify this for graphing. Any help here would be greatly appreciated.