Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > GD::Graph Invalid Data from DBI

Reply
Thread Tools

GD::Graph Invalid Data from DBI

 
 
alexbarham@yahoo.ca
Guest
Posts: n/a
 
      07-10-2005
I am having a problem querying a database and graphing the data using
GD::Graph::lines. Here is the code that I am using:

my $count=0;
my @headers=[1,2,3,4,5];
my @float_values=[32.20,32.25,32.50,32.75,33.00];
my @working_combined_array=();
my @dates=();
my @values=();
my @combined_array=();

push(@working_combined_array,@headers);
push(@working_combined_array,@float_values);

$dbh=DBI->connect("DBI:mysql:host=localhost;database=xxxxx" ,
'xxxxx','xxxxxx',
{PrintError=>0,RaiseError=>1});

$sth=$dbh->prepare( "SELECT date,close FROM table");
$sth->execute();

while(my @values=$sth->fetchrow_array())
{
push(@dates,$date_values[0]);
push(@values,$push_values[1]);
}

$sth->execute();
$sth->finish();
$dbh->disconnect();

push(@combined_array,@dates);
push(@combined_array,@values);

for ($count=0;$count1 < $date_count;$count++){
print $dates[$count],"\n";
print $values[$count],"\n";
}

my $chart_image=$chart->plot(\@combined_array) or die $chart->error;

The problem is if I use the hard-coded values, GD:Graph works fine.
However, if I use the queried data, I get an invalid data set: 0. The
for loop prints the data correctly as though there are two separate set
of data. I tried using a two dimensional array as such:

$i = 0;
while (@results = $sth->fetchrow_array ())
{
$x = $results[0];
$y = $results[1];
@points = ($x, $y);
$data[$i] = \@points;
$i++;
}
my $image = $plot->plot(\@data);

But this interspersed the dates and values on the x and y axes. If I
print a count of elements for the working_combined_array, I get 2. But
if I print a count of elements for the combined_array from the database
data, I get 10 (there are 5 rows in the table). It seems that my code
is flattening the array but I am using the same syntax of 2
push(@combined_array,@datasets) statements. I am stumped as to why this
is doing this and am wondering if anyone can spot the problem.
Thanks

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-10-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in news:1120980526.043734.266850
@g14g2000cwa.googlegroups.com:

> I am having a problem querying a database and graphing the data using
> GD::Graph::lines. Here is the code that I am using:


<code snipped>

Global symbol "@date_values" requires explicit package name at D:\Home
\ttt.pl line 26.
Global symbol "@push_values" requires explicit package name at D:\Home
\ttt.pl line 27.

Post real code.

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
Fabian Pilkowski
Guest
Posts: n/a
 
      07-10-2005
* (E-Mail Removed) schrieb:

> I am having a problem querying a database and graphing the data using
> GD::Graph::lines. Here is the code that I am using:


I'm not familiar with this module, but ...

>
> my $count=0;
> my @headers=[1,2,3,4,5];
> my @float_values=[32.20,32.25,32.50,32.75,33.00];


.... with brackets "[]" you create a reference to an anonymous array.
Your array @header contains only one element -- that reference. Try:

my @headers = ( 1, 2, 3, 4, 5 );

or just

my @headers = 1 .. 5;

Same for @float_values.

> my @working_combined_array=();
> my @dates=();
> my @values=();
> my @combined_array=();


There's no need to assign an empty list to arrays. In Perl, that's the
default for arrays.

my @working_combined_array;
my @dates;
# ...

Or do it all in one:

my( @working_combined_array, @dates );

>
> push(@working_combined_array,@headers);
> push(@working_combined_array,@float_values);
>
> $dbh=DBI->connect("DBI:mysql:host=localhost;database=xxxxx" ,
> 'xxxxx','xxxxxx',
> {PrintError=>0,RaiseError=>1});
>
> $sth=$dbh->prepare( "SELECT date,close FROM table");
> $sth->execute();
>
> while(my @values=$sth->fetchrow_array())
> {
> push(@dates,$date_values[0]);
> push(@values,$push_values[1]);


Err, is this really that code you're running? Please use "use strict"
and "use warnings" even in small examples. Do you mean $values[0] and
$values[1] instead?

> }
>
> $sth->execute();
> $sth->finish();


There's no need to call execute() again, and finish() will be done
automatically. Just drop those two lines.

> $dbh->disconnect();


[...]

regards,
fabian
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Invalid URI: There is an invalid sequence in the string. Error!! Patrick.O.Ige ASP .Net 1 07-02-2006 11:21 AM
DBI Mysql storing DBI:binary, trouble with character 26 Jerome Hauss Ruby 0 10-13-2004 03:04 PM
DBI and DBI::Oracle packages configuration ulloa Perl 1 07-22-2004 05:52 PM
DBI problem : How can I load quickly one huge table with DBI ??. Tim Haynes Perl Misc 3 09-13-2003 03:43 AM
Mason, DBI, and DBI::Pg Asby Perl Misc 0 07-24-2003 09:04 PM



Advertisments