Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: fetch all data

by stevieb (Canon)
on Mar 17, 2019 at 15:13 UTC ( [id://1231361]=note: print w/replies, xml ) Need Help??


in reply to fetch all data

I'm pretty sure you'll want to use something like fetchall_arrayref() (or one of the other "fetchall" methods):

my $records = $get_age->fetchall_arrayref; for my $record (@$records){ printf "%d\n", $record->[0]; # AGE field }

In other words, the fetchrow methods literally only grab a single row.

Update: modified code per poj's post here. I apologize for forgetting to say my code was untested.

Replies are listed 'Best First'.
Re^2: fetch all data
by bigup401 (Pilgrim) on Mar 17, 2019 at 15:54 UTC

    the results comes null. i get nothing

    my $get_age = $DBH->prepare("SELECT DISTINCT AGE FROM USERS WHERE NAME +S = ?"); $get_age->execute('John'); my $ages = $get_age->fetchall_arrayref; for my $age (@{ $ages }){ print $age; }
      fetchall_arrayref returns a reference to an Array of Arrays (AOA).

      my $sth = $DBH->prepare(' SELECT DISTINCT AGE FROM USERS WHERE NAMES = ?'); $sth->execute('John'); my $records = $sth->fetchall_arrayref; for my $record (@$records){ printf "%d\n", $record->[0]; # AGE field }

      If that doesn't work try the same code without the WHERE statement.

      my $sth = $DBH->prepare(' SELECT DISTINCT AGE FROM USER'); $sth->execute();
      poj

        Good catch. It's been quite some time since I've dabbled with DB stuff. I should have made it clear that my code was untested.

        i tried that earlier, but i think it has been my problem to not diagno +se the problem. until i have added print "Content-type: text/html\n\n" and print to rest to the top of sc +ript and am getting the results the actual results i want in header. +but not in html. in html am just getting only 1 results
        results 35 56 Content-type: text/html # this comes very well but. cant use it +on top of script for production use Age list 35 # am getting only this in html and i want to get all like in header
        #!/usr/bin/perl -w print "Content-type: text/html\n\n"; use DBI; my $host = "localhost"; my $usr = "root"; my $pwd = ""; my $dbname = "test"; my $DBH = DBI->connect("DBI:mysql:$dbname:$host", $usr, $pwd, { AutoCommit => 0, RaiseError => 1, }) or die $DBI::errstr; my $sth = $DBH->prepare(' SELECT DISTINCT AGE FROM USERS WHERE NAMES = ?'); $sth->execute('John'); my $records = $sth->fetchall_arrayref; for my $record (@$records){ print $rest = $record->[0]; } print "Content-type: text/html\n\n"; print <<START_HTML; <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <p>Age list </p> <p>$rest </p> </body> </html> START_HTML

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1231361]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found