Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I'm trying to write what should be a fairly straightforward sub to read 0 to n rows from a database table and return the data as an array of hashes (sub getAllRows below). The problem is, it doesn't return any data, so I tested using sub getOneRow, which works as expected, proving the dereferencing is correct. The next test was sub getTwoRows, which returns the same row twice. What am I doing wrong?

Note Ė Iím not worried about a fix for sub getTwoRows, as itís only a test routine to see what happens.

# $dbh already connected using DBI @data = &getOneRow($find); %row = $data[0]; $field1 = $row->{field1}; $field2 = $row->{field2}; $field3 = $row->{f +ield3}; print "Row id is $field1, field 2 is $field2, field 3 is $field3\n"; # Prints valid, correct values for the first matching row @data = &getTwoRows($find); %row = $data[0]; $field1 = $row->{field1}; $field2 = $row->{field2}; $field3 = $row->{f +ield3}; print "Row id is $field1, field 2 is $field2, field 3 is $field3\n"; %row = $data[1]; $field1 = $row->{field1}; $field2 = $row->{field2}; $field3 = $row->{f +ield3}; print "Row id is $field1, field 2 is $field2, field 3 is $field3\n"; # Prints valid, correct values for the first row, but the second print + line gives the first row repeated @data = &getOneRow($find); foreach %row (@data) { $field1 = $row->{field1}; $field2 = $row->{field2}; $field3 = $row +->{field3}; print "Row id is $field1, field 2 is $field2, field 3 is $field3\n +"; } # Prints no values for any of the rows ("Row id is , field 2 is , fiel +d 3 is ") sub getOneRow { my $find = shift; my ($sql, $sth); $sql = "SELECT field1, field2, field3 FROM mytable WHERE number = +$find ORDER BY field1"; $sth = $dbh->prepare($sql) or die "Couldn't prepare: " . $dbh->err +str(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); my @data = (); $rowref = $sth->fetchrow_hashref(); push @data, %{$rowref}; return @data; } sub getTwoRows { my $find = shift; my ($sql, $sth); $sql = "SELECT field1, field2, field3 FROM mytable WHERE number = +$find ORDER BY field1"; $sth = $dbh->prepare($sql) or die "Couldn't prepare: " . $dbh->err +str(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); my @data = (); $rowref = $sth->fetchrow_hashref(); push @data, %{$rowref}; $rowref = $sth->fetchrow_hashref(); push @data, %{$rowref}; return @data; } sub getAllRows { my $find = shift; my ($sql, $sth); $sql = "SELECT field1, field2, field3 FROM mytable WHERE number = +$find ORDER BY field1"; $sth = $dbh->prepare($sql) or die "Couldn't prepare: " . $dbh->err +str(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); my @data = (); while ( $rowref = $sth->fetchrow_hashref() ) {push @data, %{$rowr +ef};} return @data; }

In reply to Array of Hashes by Ken_M

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (3)
    As of 2019-10-19 14:27 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?