Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

problem with returning all records

by frank1 (Scribe)
on Apr 16, 2024 at 10:30 UTC ( [id://11158887]=perlquestion: print w/replies, xml ) Need Help??

frank1 has asked for the wisdom of the Perl Monks concerning the following question:

My script below works, but i have one problem, it just display out 1 record

What i need is to return all records from database

my $sth = $dbh->prepare("SELECT snd.name, snd.country, m.item, m.eqty, m.price, m.line, m.prodct FRO +M buys as m JOIN Busers as snd ON snd.id = m.buyer WHERE CASE WHEN (SELECT SUM(s +tatus = ?) FROM buys) > 0 THEN m.status = ? ELSE m.status = ? OR m. +status = ? OR m.status = ? END ORDER BY created ASC"); $sth->execute('sold', 'forsell', 'bugt', 'paid', 'inshop'); my $Data = $sth->fetchall_arrayref(); foreach my $Data (@$Data) { my ($name, $country, $item, $eqty, $price, $line, $prodct) = @$Dat +a; $html = "<tr> <td> <p>$name</p> <p> $eqty, $price, $line, </p> $prodct $country </td> </tr> "; }

Replies are listed 'Best First'.
Re: problem with returning all records
by hippo (Bishop) on Apr 16, 2024 at 11:21 UTC

    What, no SSCCE?

    #!/usr/bin/env perl use strict; use warnings; my $AoA = [ [qw/a b/], [qw/c d/] ]; my $out = ''; foreach my $aref (@$AoA) { $out = "@$aref\n"; } print $out; # only prints "c d", of course

    🦛

Re: problem with returning all records
by marto (Cardinal) on Apr 16, 2024 at 11:04 UTC

    Your code doesn't display this anywhere, you've not shown where you define $html or print it. You're clobbering (not appending to) $html so if you print it outside of the foreach loop it will only have one result.

      thanks so much, very good and clean answer

      its now working. i was printing the $html outside the foreach loop, am now printing $html inside the foreach loop and its working

      Can i have a solution for this

      am getting results like this :

      {"msg":"data "} {"msg":"data "} and so on.........

      i want to return the whole data to single json like:

      {"msg":" all data goes here"}
      my $sth = $dbh->prepare("SELECT snd.name, snd.country, m.item, m.eqty, m.price, m.line, m.prodct FRO +M buys as m JOIN Busers as snd ON snd.id = m.buyer WHERE CASE WHEN (SELECT SUM(s +tatus = ?) FROM buys) > 0 THEN m.status = ? ELSE m.status = ? OR m. +status = ? OR m.status = ? END ORDER BY created ASC"); $sth->execute('sold', 'forsell', 'bugt', 'paid', 'inshop'); my $Data = $sth->fetchall_arrayref(); foreach my $Data (@$Data) { my ($name, $country, $item, $eqty, $price, $line, $prodct) = @$Dat +a; $html = "<tr> <td> <p>$name</p> <p> $eqty, $price, $line, </p> $prodct $country </td> </tr> "; my $json = encode_json( { msg => $html} ); print $q->header( -type => 'application/json' ),$json; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11158887]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-05-20 14:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found