Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7953457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:09:47+00:00 2026-06-04T03:09:47+00:00

I am trying to create a table using CGI.pm. The table would contain results

  • 0

I am trying to create a table using CGI.pm. The table would contain results from
querying a database. The array final contains record numbers on which the queries will
be executed. The results for each record should be in a separate row. The result
of each query on the record should form a column in the row. Using some code I found
here, I wrote the following
(which may not be he best way of doing it):

#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use DBI;

...

my @rows;
my $rec;

foreach my $el (@final) {
              @rows = ();
    foreach my $query (@queries) {
        my $query_handle = $connxn->prepare($query);
        $query_handle->execute($el);
        $query_handle->bind_columns(undef, \$rec);
        while($query_handle->fetch()) {
            push(@rows,td($rec));
        }   
    }
    print $q->table({-border=>undef,-width=>'25%'},
        Tr(\@rows)
       );
}

When I run this, I get a blank page and an error “Undefined subroutine &main::td”
in the error log. Why do I get this error and I how do I get rid of it? Is there any
other way to create a table the table I want using CGI.pm?
Any help would be appreciated. Thanks!

EDIT: Using Sinan’s suggestion, I modified my code as follows:

#!/usr/bin/perl 
use strict; 
use warnings; 
use DBI;  
use Carp; 
use HTML::Template;

...  
my @rows; 
my $rec;  
foreach my $el (@final) {     
    foreach my $query (@queries) {         
        my $query_handle = $connxn->prepare($query);         
        $query_handle->execute($el);         
        $query_handle->bind_columns(undef, \$rec);         
        while($query_handle->fetch()) {             
            push(@rows, { CELLS => [ map { CELL => $_ }, $rec ] });         
        }        
    } 
}

my $tmpl = HTML::Template->new(filehandle => \*DATA);
$tmpl->param(ROWS => \@rows); 
print $tmpl->output;

__DATA__ 
<table> 
<TMPL_LOOP ROWS> 
<tr> 
<TMPL_LOOP CELLS> 
<td><TMPL_VAR CELL></td> 
</TMPL_LOOP>
</tr> 
</TMPL_LOOP>
</table>

But now I am getting the result of each query in a separate row, instead of the same one
(see original question above). I can’t seem to fix it on my own. Any ideas?

EDIT2: Though I am accepting Sinan’s answer, meanwhile I developed my own solution (in case anyone is interested):

my @rows;
my @rows1;
foreach my $el (@final) {
    @rows = ();
    foreach my $query1 (@queries) {
        my $query_handle = $connxn->prepare($query1);
        $query_handle->execute($el);
        $query_handle->bind_columns(undef, \$rec);
        while($query_handle->fetch()) {
            push(@rows, $rec);              
        }                       
    }
    push(@rows1, { CELLS => [ map { CELL => $_ }, @rows ] }); 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:09:49+00:00Added an answer on June 4, 2026 at 3:09 am

    I know you think you want to use CGI.pm to generate the tables, but you shouldn’t. The code will be ugly and it will hurt.

    Instead, separate presentation from logic. Avoid the HTML generation functions of CGI.pm. Use a simple templating module such as HTML::Template. Customize the HTML to your heart’s content without messing with your code.

    Given the update to your question, I think what you need is this:

    foreach my $el (@final) {
        my @cells;
        foreach my $query (@queries) {         
            my $query_handle = $connxn->prepare($query);         
            $query_handle->execute($el);         
            $query_handle->bind_columns(undef, \$rec);
            while($query_handle->fetch()) {
                push @cells, { CELL => $rec };
            }        
        } 
        push @rows, { CELLS => \@cells };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a table in my database through C# using the following
I'm trying to create an XML sitemap using CakePHP, from a table which has
So I'm trying to create a federated table using the syntax from the docs
I'm trying to create a table of information in a database query using Zend
I am trying to create a database table using PostgreSQL but I am unable
I am trying to create a generic table editor using Entity Framework. I would
I'm trying to create a simple database table using the PHP MySQL query Create
I am trying to create a table in Drupal database using hook_schema function but
I'm trying to create a table in a database using SQL. The data is
I am trying to create a table in MySql using php. My code looks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.