在Perl/Mysql中显示内容及其相关内容

在Perl/Mysql中显示内容及其相关内容,mysql,perl,Mysql,Perl,我正试图从一个像这样的小Perl项目中获得这个示例输出 content1 relatedcontent1 relatedcontent2 relatedcontent2 content2 relatedcontent1 relatedcontent2 这是我的密码 #!C:/Perl64/bin/perl.exe use strict; use warnings; use v5

我正试图从一个像这样的小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();

它将只打印内容之外的一个相关内容。每一个内容和它自己至少有3个相关内容。

你已经声明了两个子程序,并且你正在调用一点零一分,所以它是这样执行的,例如,考虑下面的一个< /P>
sub1();
sub2();

sub sub1()
{
    for(0..5)
    {
        print "hello\n";
        sub sub2()
        {
            print "hi\n";

        }
    }
}

#output
hello
hello
hello
hello
hello
hello
hi
因此,您应该删除子查询\u person\u by\u target子例程,或者在父子例程query\u赞助商内调用子例程,并在子和循环外声明子例程,如下所示未测试

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();

在程序中的其他代码中间定义子程序。这让我觉得你期望它们按照定义执行,但事实并非如此——它们按照你所说的方式执行。你这样称呼他们:

query_sponsor($dbh);
query_person_by_target($dbh);
因此,毫不奇怪,您会从query\u赞助商获得所有输出,然后是query\u person\u的所有输出

更好的方法是从查询发起人内部通过查询目标调用查询人。我还将其分为两个阶段-第一阶段将数据提取到数据结构中,第二阶段显示该数据

由于我没有您的数据库,此代码未经测试

#!/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}";
  }
}
我还建议您考虑使用模板工具包之类的工具来生成输出。将原始HTML放入Perl程序是一个糟糕的想法-它肯定会变成无法维护的混乱:-

#!/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}";
  }
}