in reply to Re: Re: GD::Graph::lines problem!
in thread GD::Graph::lines problem!
First, there has to be a better way to do this:
I'd prefer a query that asks for names of tables (I don't know mysql so much, but in oracle, I'd do "select table_name from user_tables" -- 'user_tables' is an oracle-internal table, the same on every server, that lists information on all the user tables in the current user account). If you really want to do this via a back-tick command, and your table names are always numeric dates with underscores, try:@dates = `mysqlshow --password=\'xxxxxxx\' pzbg001`; shift @dates; shift @dates; shift @dates; shift @dates; pop @dates; foreach $date(@dates){ chomp $date; $date =~ s/ //g; $date =~ s/\|//g; $date =~ s/\-//g; $date =~ s/Tables//g; $date =~ s/\+//g; ...
(For that matter, I'd use "@tables" as the array name there.)@dates = map { /^\|\s+([\d_]+)\s+\|$/ } `mysqlshow --...`
Next, building the query is simpler than you seem to think:
Now, I was about to say you have the "sth->finish" call inside the while loop instead of outside (where it belongs), but then I realized that your indentation is still out of whack, making it harder to understand the code.$query = "select $result from $date where inintf=$inint";
Another thing that's a little misleading here is that you are, in effect, running the same query on several tables, and each time you run it, you get exactly one scalar value, but you use a "while" loop with a nested "foreach" loop to retrieve that single value.
And finally, getting to the real problem, you are probably doing the wrong thing with @data = [\@dates, \@rage] -- the plot function probably wants an array of data points to graph, where each point is an array having one X coord.value and one Y coord.value (but I don't know GD at all -- you should check the docs on that). Anyway, if my guess is right, you should try it like this (this is the whole thing of doing the queries and assembling the array for plotting):
update: added some commentary to the code; also added some more "my" scopers.# get the list of tables (table names are \d+_\d+_\d+): my @dates = map { /^\|\s+([\d_]+)\s+\|$/ } `mysqlshow --...` # query each table for sum of traffic, # push table_name (i.e. date) and traffic sum onto @data: my @data = (); for my $date ( @dates ) { my $query = "select $result from $date where inintf=$inint"; my $sth = $dbh->prepare($query); if (!$sth) { die "Illegal query: $query" }; $sth->execute; my $octets = ($sth->fetchrow_array)[0]; # don't chomp it! $sth->finish; push @data, [ $date, $octets/(1024*1024) ]; } # do all that other GD stuff... then: my $myimage = $mygraph->plot(\@data) or die $mygraph->error; # and on to the web page...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: GD::Graph::lines problem!
by Baiul (Acolyte) on May 11, 2003 at 10:08 UTC | |
by Baiul (Acolyte) on May 11, 2003 at 11:04 UTC |