我正在尝试从像这样的小型Perl项目中获得此示例输出
content1
relatedcontent1
relatedcontent2
relatedcontent2
content2
relatedcontent1
relatedcontent2
这是我的代码
#!C:/Perl64/bin/perl.exe
use strict;
use warnings;
use v5.10; # for say() function
use DBI;
use HTML::Table;
# MySQL database configurations
my $dsn = "DBI:mysql:naxum";
my $username = "root";
my $password = '';
print "Content-Type:text/html\r\n\r\n";
# connect to MySQL database
my %attr = ( PrintError=>0, # turn off error reporting via warn()
RaiseError=>1 # report error via die()
);
my $dbh = DBI->connect($dsn,$username,$password,\%attr);
# query data from the sponsor table
query_sponsor($dbh);
query_person_by_target($dbh);
sub query_sponsor{
# query from the table
my ($dbh) = @_;
my $sql = "SELECT name,id FROM sponsor";
my $sth = $dbh->prepare($sql);
# execute the query
$sth->execute();
print "<table>\n";
print "<thead>\n";
print "<tr>\n";
print "<th>Id</th>\n";
print "<th>Name</th>\n";
print "</tr>\n";
print "</thead>\n";
print "<tbody>\n";
while(my @row = $sth->fetchrow_array()){
print "<tr>\n";
print "<td>\n";
print $row['1'];
sub query_person_by_target{
my ($dbhPerson) = @_;
my $sqlPerson = "SELECT username, firstname FROM person WHERE sponsor_id = ?";
my $sthPerson = $dbhPerson->prepare($sqlPerson);
$sthPerson->execute($row['1']) or die "execution failed: $dbhPerson->errstr()";
while ( my @rowPerson = $sthPerson->fetchrow_array()){
print "<p>$rowPerson['0']</p>\n";
}
$sth->finish();
}
print "</td>\n";
print "<td>$row['0']</td>\n";
print "</tr>\n";
}
$sth->finish();
print "</tbody>\n";
print "</table>\n";
}
$dbh->disconnect();
然而,我无法得到我想要实现的输出。结果如下
content1
content2
content3
.....
relatedcontent1
它只会在内容之外打印一个相关内容。每个内容及其自身至少有3个相关内容。
答案 0 :(得分:0)
您可以在程序中的其他代码中间定义子例程。这让我觉得你希望它们在定义时执行,但事实并非如此 - 它们在你调用它们时执行。你称之为:
query_sponsor($dbh);
query_person_by_target($dbh);
因此,获得query_sponsor()
的所有输出后跟query_person_by_name()
的所有输出就不足为奇了。
更好的方法是从query_person_by_target()
内拨打query_sponsor()
。我还将其分为两个阶段 - 第一阶段将数据提取到数据结构,第二阶段显示数据。
由于我没有您的数据库,因此该代码未经测试。
#!/usr/bin/perl
use strict;
use warnings;
use feature 'state'; # for state variables
use feature 'say';
use CGI 'header';
sub get_dbh {
# MySQL database configurations
my $dsn = "DBI:mysql:naxum";
my $username = "root";
my $password = '';
# connect to MySQL database
my %attr = (
PrintError=>0, # turn off error reporting via warn()
RaiseError=>1, # report error via die()
);
return DBI->connect($dsn, $username, $password, \%attr);
}
sub query_sponsor {
state $dbh = get_dbh();
my $sql = 'select name, id from sponsor';
my $sth = $dbh->prepare($sql);
$sth->execute;
my @sponsors;
while (my @row = $dbh->fetchrow_array) {
push @sponsors, {
name => $row[0],
id => $row[1],
people => get_people_for_sponsor($row[1]),
};
}
return @sponsors;
}
sub get_people_for_sponsor {
my ($sponsor_id) = @_;
state $dbh = get_dbh;
my $sql = 'select username, firstname
from person
where sponsor_id = ?';
my $sth = $dbh->prepare;
$sth->execute($sponsor_id);
my @people;
while (my @row = $sth->fetchrow_array) {
push @people, {
username => $row[0],
firstname => $row[1],
};
}
return \@people;
}
my @sponsors = query_sponsor();
# Now you have all of your data in @sponsors. You simply need
# to walk that array and use the data to build the output you
# want. My example is plain text - it shouldn't be too hard to
# convert it to HTML.
print header('text/plain');
for my $s (@sponsors) {
say "$s->{id}: $s->{name}";
for my $p (@{$s->{people}}) {
say "* $p->{firstname} / $p->{username}";
}
}
我还建议您使用Template Toolkit之类的东西来生成输出。将原始HTML放入Perl程序是一个可怕的想法 - 它可以保证变成一个无法维护的混乱: - )
答案 1 :(得分:-1)
根据你所说的声明,是正确的。
你已经声明了两个子程序,并且你一个接一个地调用它,所以它正在执行,例如考虑下面的一个
sub1();
sub2();
sub sub1()
{
for(0..5)
{
print "hello\n";
sub sub2()
{
print "hi\n";
}
}
}
#output
hello
hello
hello
hello
hello
hello
hi
所以你应该删除子query_person_by_target
子程序或在子子程序query_sponsor
内调用子子程序,并在sub和loop之外声明子子程序,如下(未测试)
query_sponsor($dbh);
sub query_sponsor
{
# query from the table
my ($dbh) = @_;
my $sql = "SELECT name,id FROM sponsor";
my $sth = $dbh->prepare($sql);
# execute the query
$sth->execute();
print "<table>\n";
print "<thead>\n";
print "<tr>\n";
print "<th>Id</th>\n";
print "<th>Name</th>\n";
print "</tr>\n";
print "</thead>\n";
print "<tbody>\n";
while(my @row = $sth->fetchrow_array())
{
print "<tr>\n";
print "<td>\n";
print $row['1'];
query_person_by_target($dbh);
print "</td>\n";
print "<td>$row['0']</td>\n";
print "</tr>\n";
}
$sth->finish();
print "</tbody>\n";
print "</table>\n";
}
sub query_person_by_target{
my ($dbhPerson) = @_;
my $sqlPerson = "SELECT username, firstname FROM person WHERE sponsor_id = ?";
my $sthPerson = $dbhPerson->prepare($sqlPerson);
$sthPerson->execute($row['1']) or die "execution failed: $dbhPerson->errstr()";
while ( my @rowPerson = $sthPerson->fetchrow_array()){
print "<p>$rowPerson['0']</p>\n";
}
$sth->finish();
}
$dbh->disconnect();