简体   繁体   English

在perl cgi中显示浏览器中的值

[英]display value in browser in perl cgi

I am able to fetch a data from db not able to display in browser. 我能够从db中获取无法在浏览器中显示的数据。 below is the code- 以下是代码 -

my $q = CGI->new;
print $q->header,$q->start_html('testing');
my $title = $q->param('title');
my $perl = "";

#these is displayed properly
print "<font color=blue><b>TITLE:\"$title\"</b><br>";
print "<font color=blue><b>SCRIPT:\"$title\"</b>\n";

my $dbh = DBI->connect("DBI:ODBC:test","username","password") || die "Connection error: $DBI::errstr\n";
my $sql = "select * from tablename where title = '$title'";
my $sth = $dbh->prepare($sql);
$sth->execute;
my @row = $sth->fetchrow_array;
for(my $i=1;$i<=@row;$i++)
{
    if($i == 5)
    {
    $perl = "$row[$i]";
    }
}

#below is not displayed in browser
print $q->strong($title);
print $q->strong($perl);

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

print $q->end_html;

I just want to print the value of $title and $perl in browser. 我只想在浏览器中打印$ title和$ perl的值。 this program is running properly but cant able to display value of $title and $perl 这个程序运行正常,但无法显示$ title和$ perl的值

The reason for the failure is not obvious to me, but you should use placeholders when performing queries: 失败的原因对我来说并不明显,但在执行查询时应使用占位符

my $sql = "select * from tablename where title = ?";  # placeholder
my $sth = $dbh->prepare($sql);
$sth->execute($sql);                                  # $sql is used here

The placeholder is a question mark ? 占位符是一个问号? . This will ensure that your values are quoted properly, and prevent injection attacks. 这将确保正确引用您的值,并防止注入攻击。 Using the data from the CGI object without sanitizing it is very dangerous. 使用来自CGI对象的数据而不进行消毒是非常危险的。

Also, it seems that you are only taking one value from the array, so there is little need to use a loop in the first place. 此外,您似乎只从数组中获取一个值,因此几乎不需要首先使用循环。 You could just do: 你可以这样做:

my $row = $row[5];

To see if the value was in the database, you can use if (defined $row) , or if (@row >= 6) . 要查看值是否在数据库中,您可以使用if (defined $row)if (@row >= 6) (Note that arrays start at 0, so the element with index 5 is actually the 6th element. Just pointing this out since you started your loop at 1.) (注意,数组从0开始,所以索引为5的元素实际上是第6个元素。自从你在1开始循环以来,指出这个元素。)

Try running it straight from the command line, without the browser. 尝试直接从命令行运行它,无需浏览器。

See here and here . 看到这里这里

You can also use the Perl debugger, if you start it with: 您也可以使用Perl调试器,如果您启动它:

perl -d yourprogram

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM