in reply to HTML::Template-Displaying DB Records

Simplify. You seem to have debugged it down to one section of code involving a database fetch. Take that, run it on its own, and debug it until it works. The use of selectall_arrayref with the Columns => {} attribute looks suspicious to me. I would fiddle with that.

You didn't ask for general comments, but there are at least a couple of things in this script that will jump out at any experienced coder. The first is the lack of "use strict" at the top. That is a must. The second is that you are not using bind parameters with your SQL statements. You really should, since it is simpler and safer than all of the quoting you have right now. Kudos for using a template though. That's a step in the right direction.

  • Comment on Re: HTML::Template-Displaying DB Records

Replies are listed 'Best First'.
Re: HTML::Template-Displaying DB Records-Still struggling
by thisisperl (Novice) on May 25, 2004 at 18:49 UTC
    Alright.... I know my template code is fine. I tried:
    my $rows=[ # { # TicketNo => 1, # Customer => "Foo", # UserName => "Foo", # DateSubmission =>"Foo", # Via =>"Foo", # Medium =>"Foo", # Issues =>"Foo", # SupportType =>"Foo", # Description =>"Foo", # Detail =>"Foo", ## AssignTo =>"Foo", # Status =>"Foo", # Remarks =>"Foo", # Priority =>"Foo", # ETD =>"Foo", # ClosedDate =>"Foo", # ClosedName =>"Foo" ## }, ## { # TicketNo => 2, # Customer => "Blah", # UserName => "Blah", # DateSubmission => "Blah", # Via => "Blah", # Medium => "Blah", # Issues => "Blah", # SupportType => "Blah", # Description => "Blah", # Detail => "Blah", # AssignTo => "Blah", # Status => "Blah", # Remarks => "Blah", # Priority => "Blah", # ETD => "Blah", # ClosedDate => "Blah", # ClosedName => "Blah" # } # # ]; #$template->param(ROWS => $rows || []);
    And the rows displayed in the template. The problems seems to be with selectall_arrayref. But when I print $rows, an address is displayed-I just think $rows is empty or something. I cannot for the life of me figure out how to test selectall_arrayref. And yes I tried:
    my $rows = $dbh->selectall_arrayref("SELECT TicketNo, Customer, UserNa +me, DateSubmission, Via, Medium, Issues, SupportType, Description, De +tail, AssignTo, Status, Remarks, Priority, ETD, ClosedDate, ClosedNam +e FROM TSIssuesTable WHERE Customer LIKE $quotedString1 AND Priority +LIKE $quotedString2 AND TicketNo LIKE $quotedString3 AND DateSubmissi +on LIKE $quotedString4");
    Removed the COlumns=>{}-But it makes no difference. What can I do to debug further? How can I check whether selectall_arrayref returns something (besides how I am doing it now). And I must mention here that I am relatively new to perl-about 2-3 months old. About place binders Please help me solve this problem.
      Here is how you examine a complex data structure:
      use Data::Dumper; print Dumper $rows;
      That will show you what's in it.

      Honestly, I have never used the convenience functionns in DBI like selectall_arrayref, so I don't know if you are using it right. Why don't you try going one step lower and calling prepare, execute, and fetchall_arrayref separately?

        Thanks. I tried Data::Dumper and here's the output:
        <br> use Data:: Dumper; <br> print Dumper $dbh; <i>###This displays $VAR1 = bless( {}, 'DBI::db' +); -does this mean DB connection was established? (do need an answer +here)</i> <br> print $rows; <i>###This displays ARRAY(0x1ed0ad8)</i> <br> print Dumper $rows; <i>### This displays $VAR1 = []; which means the a +rray reference is empty-but why?</i> <br>
        I also tried fetchrow_hashref():
        my $rows=[]; $sth = $dbh->prepare( $SQL ); $sth->execute(); push @{$rows}, $_ while $_ = $sth->fetchrow_hashref(); print Dumper $dbh; #Prints $VAR1 = bless( {}, 'DBI::db' ); print Dumper $sth;#Prints $VAR1 = bless( {}, 'DBI::st' ); print Dumper $rows; #Prints $VAR1 = [];
        Same problem. Surprisingly, if you look at the code in my previous posts, I used selectcol_arrayref successfully in the retrieveFormData function so the DB connection seems to be fine. What could possibly be wrong?