GD:进行DBI连接时,Graph没有输出

时间:2014-12-14 01:34:06

标签: perl sqlite dbi gd-graph

我正在小型服务器上工作,以提供包含与家庭自动化设置相关的数据的网页。我有一个充满传感器数据的数据库,如果我取消评论底部的部分,我可以成功查询并输出到图形图像。我还可以通过浏览器作为cgi脚本访问它来打印图形,方法是将sqlite代码替换为在中间附近注释掉的手动创建的out1和out2。问题在于尝试将它们放在一起并在浏览器中将图形化的sqlite数据视为cgi脚本。我已将问题缩小到连接代码。当我将它添加到其他工作脚本时,你没有图像输出;但我知道连接代码有效。它必须是显而易见的东西,但我无法找到它。想法?

#!/usr/bin/perl -w
use CGI ':standard';
use DBI;
use GD::Graph::lines;
use strict;

my $dbh = DBI->connect(          
    "dbi:SQLite:dbname=/root/distancetest", 
    "",
    "",
    { RaiseError => 1}
) or die $!;


my $sth = $dbh->prepare("SELECT * FROM distance");
$sth->execute();

my $row;
my @data;
my @out1;
my @out2;

while ($row = $sth->fetchrow_arrayref()) {
    push(@out1, @$row[0]);
    push(@out2, @$row[1]);
}
#@out1 = (1,2,3,4,5);
#@out2 = (1,2,3,4,5);

$data[0] = \@out1;
$data[1] = \@out2;

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

my $skip = int ((@data * 8)/(600-50) + 1);

my $mygraph = GD::Graph::lines->new(600, 300);
$mygraph->set(
    x_label     => 'Timestamp',
    y_label     => 'cm',
    title       => 'Distance measuted at time',
    line_types  => [1],
    line_width  => 2,
    x_labels_vertical => 1,
    x_label_skip => $skip,
    dclrs       => ['blue'],
) or warn $mygraph->error;

$mygraph->set_legend_font(GD::gdMediumBoldFont);
$mygraph->set_legend('sensor1');
my $myimage = $mygraph->plot(\@data) or die $mygraph->error;

print "Content-type: image/png\n\n";
#open(OUTPUT, ">$0.png") or die "Can't open $0.png: $!\n";
#print OUTPUT $myimage->png();
print $myimage->png;

1 个答案:

答案 0 :(得分:0)

要查找代码有什么问题,可以添加以下代码:

use CGI::Carp qw(fatalsToBrowser);