http://www.perlmonks.org?node_id=1198110

shanta has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get data from MySQL into GD.

Never been good with strings arrays etc

I need to go from this.

my $sql = "SELECT time, mastuntemp, LineTemp, spargtemp FROM brew_te +mp_tb WHERE sitename = 'Brew' AND +batchnumber = '$batchnumber' ORDER BY time";

to

my @data = ( ["08:00","08:10","08:20","08:30","08:50","09:00","09:10", "09:20", + "09:30"],# time [ 160, 162, 165, 166, 163, 165, 165, 168, + 164], #mastuntemp [ 165, 162, 160, 166, 163, 165, 165, 168, + 164], #LineTemp [ 160, 170, 165, 166, 163, 165, 165, 168, + 164], #spargtemp )or die GD::Graph::Data->error;

Thanks for the help Shanta

Current state of code Gives the error code after code.

#!/usr/bin/perl use strict; use GD::Graph::bars; use GD::Graph::lines; use GD::Graph::colour; use GD::Graph::Data; use CGI qw(:standard); use DBI; my $CGI = new CGI() or die("Unable to construct the CGI object" . ". Please contact the webmaster."); foreach ($CGI->param()) { $CGI->param($1,$CGI->param($_)) if (/(.*)\.x$/); } my $SiteName = $CGI->param('site') ; my $batchnumber = $CGI ->param('batchnumber')||"20170903nervana" ; my $dsn = "ddbi stuff"; my $usr = 'username'; my $password = 'pw'; my $dbh = DBI->connect($dsn,$usr,$password,{ RaiseError => 1}); my $sql = "SELECT time, mastuntemp, LineTemp, spargtemp FROM brew_temp +_tb WHERE sitename = 'Brew' AND batchnumber = ? ORDER BY time"; print "DEBUG: \$sql = \"$sql\"\n"; my $sth = $dbh->prepare($sql); print "DEBUG: \$sth = \"$sth\"\n"; if (!$sth->execute($batchnumber)) { die "Error: ". $sth->errstr ."\n"; } my @row; my @row_array; my $data = GD::Graph::Data->new(600,500); $data->set( markers => [3, 5, 6] , dclrs => [ qw(green pink blue cyan) ], x_label => 'Time', y_label => 'Tempurature', title => "Batch $batchnumber Mashing Temperatures.", y_max_value => 180, y_tick_number => 8, y_label_skip => 2 ) or die $data->error; $data->set_legend('mashtontemp', 'LineTemp', 'spargtemp', my $recordidx = 0 ); while (@row = $sth->fetchrow_array) { print "DEBUG: \@row = \"@row\"\n"; #debug my $rowidx = 0; #degug foreach my $rowdata (@row) { #debug print " [$rowidx] = \"$rowdata\"\n";#debug $rowidx++; #debug print "DEBUG: \$data = \"$data\"\n";#debug }#debug print "DEBUG: \$data = \"$data\"\n";#debug print "record count $recordidx\n";#debug $data->add_point(@row); $recordidx++;#debug } print "DEBUG: \$data = \"$data\"\n";#debug my $chart = GD::Graph::lines->new(); my $gd = $chart->plot($data);
False [] range "\w-" in regex; marked by <-- HERE in m/Can.?t locate \ +./[\w- <-- HERE _]+\.cgi in \@INC/ at graph_debug.cgi line 78. DEBUG: $sql = "SELECT time, mastuntemp, LineTemp, spargtemp FROM brew +_temp_tb WHERE sitename = 'Brew' AND batchnumber = ? ORDER BY time" DEBUG: $sth = "DBI::st=HASH(0x1fe2d88)" DEBUG: @row = "07:40 62.50 64.00 69.40" [0] = "07:40" DEBUG: $data = "GD::Graph::lines=HASH(0x1ff1a78)" [1] = "62.50" DEBUG: $data = "GD::Graph::lines=HASH(0x1ff1a78)" [2] = "64.00" DEBUG: $data = "GD::Graph::lines=HASH(0x1ff1a78)" [3] = "69.40" DEBUG: $data = "GD::Graph::lines=HASH(0x1ff1a78)" DEBUG: $data = "GD::Graph::lines=HASH(0x1ff1a78)" record count 0 Content-type: text/html <H1>Software error:</H1> <PRE>Can't call method &quot;add_point&quot; on an undefined value at +./graph.cgi line 64.